SQL Server datetime
日历的时期(零点)为1900-01-01T00:00:00.000
。如果你喜欢做
select convert(datetime,'')
这就是你会得到的价值。
SQL Server的datetime
数据类型由2个带符号的32位整数组成。第一个是距离这个时代的偏移量;第二个是将时间表示为从开始日起以毫秒为单位的偏移量。
当你减去两个日期时间值,说@end
和@start
它,你希望它是什么。
- 如果
@end.time-of-day
是< @start.time-of-day
,从@end.date
携带一天的毫秒和减少@end.date
。
- 通过从
@end.time-of-day
减去@start.time-of-day
来计算新的时间价值。
- 通过从@减去
@start.date
end.date`
计算新日期如果所得到的值是datetime
外域(1753-01-01T00:00:00.000
通过9999-12-31T23:59:59.997
)引发错误。
您将得到预期的结果... SQL Server。
编辑来显示什么在幕后事:
declare @x datetime = '2014-02-22 00:12:00'
declare @y datetime = '2014-02-22 00:00:00'
declare @z datetime = @x - @y
select 'end' ,
date = @x ,
description = 'days_since_epoch' ,
value = convert(int,substring(convert(varbinary(8),@x) , 1 , 4)) ,
description = 'time_as_ms_offset' ,
value = convert(int,substring(convert(varbinary(8),@x) , 5 , 4))
union select 'start' ,
date = @y ,
description = 'days_since_epoch' ,
value = convert(int,substring(convert(varbinary(8),@y) , 1 , 4)) ,
description = 'time_as_ms_offset' ,
value = convert(int,substring(convert(varbinary(8),@y) , 5 , 4))
union select 'delta' ,
date = @z ,
description = 'days_since_epoch' ,
value = convert(int,substring(convert(varbinary(8),@z) , 1 , 4)) ,
description = 'time_as_ms_offset' ,
value = convert(int,substring(convert(varbinary(8),@z) , 5 , 4))
产生这样的结果,显示了数学:
date description value description value
----- ----------------------- ---------------- ----- ----------------- ------
end 2014-02-22 00:12:00.000 days_since_epoch 41690 time_as_ms_offset 216000
start 2014-02-22 00:00:00.000 days_since_epoch 41690 time_as_ms_offset 0
delta 1900-01-01 00:12:00.000 days_since_epoch 0 time_as_ms_offset 216000
0000-00-00 0点12分00秒是无效日期时间在Sql Server中。你试图找到什么样的差异。你可以根据需要找到几小时或几分钟的差异并将其转换为天数? – NoviceProgrammer