在SQL SERVER中时间的精度为1/300秒,换句话说在SQL SERVER中1代表1/300S,所以1S在SQL SERVER中存储时对应的十进制数据是300,十六进制为12C。300是这样的到的:1除以1/300。 那么在SQL SERVER中的其他的ms如何表示呢? 在下面的例子中,使用一个循环在表中插入1000行,每行分别增加1ms,但是在SQL SERVER中表示并不是按照0.001,0.002,0.003等来表示的,而是进行了相应的处理。
declare @tb table(id int identity(0,1),date datetime)
declare @count int,@i floatset @count=0
while(@count<=1000) begin
insert into @tb values(dateadd(ms,@count,'00:00:00.000'))
set @count=@count+1
end
select id,date ,cast(date as binary(8)) as [binary] ,cast(cast(date as binary(8)) as int) as [int]from @tb
结果为:id date binary int
0 1900-01-01 00:00:00.000 0x0000000000000000 0
1 1900-01-01 00:00:00.000 0x0000000000000000 0
2 1900-01-01 00:00:00.003 0x0000000000000001 1
3 1900-01-01 00:00:00.003 0x0000000000000001 1
4 1900-01-01 00:00:00.003 0x0000000000000001 1
5 1900-01-01 00:00:00.007 0x0000000000000002 2
6 1900-01-01 00:00:00.007 0x0000000000000002 2
7 1900-01-01 00:00:00.007 0x0000000000000002 2
8 1900-01-01 00:00:00.007 0x0000000000000002 2
9 1900-01-01 00:00:00.010 0x0000000000000003 3
10 1900-01-01 00:00:00.010 0x0000000000000003 3
999 1900-01-01 00:00:01.000 0x000000000000012C 300
1000 1900-01-01 00:00:01.000 0x000000000000012C 300
对于0.000来说对应的二进制数字为0,那么对应的数字为0 ,
0.001来说对应的二进制数字如何得到呢?1/1000除以1/300得到0.3四舍五入为0,那么对应的数字为0
0.002来说对应的二进制数字如何得到呢?2/1000除以1/300得到0.6四舍五入为1,那么对应的数字为1
0.003来说对应的二进制数字如何得到呢?3/1000除以1/300得到0.9四舍五入为1,那么对应的数字为1
0.004来说对应的二进制数字如何得到呢?4/1000除以1/300得到1.2四舍五入为1,那么对应的数字为1
0.005来说对应的二进制数字如何得到呢?5/1000除以1/300得到1.5四舍五入为2,那么对应的数字为2
0.006来说对应的二进制数字如何得到呢?6/1000除以1/300得到1.8四舍五入为2,那么对应的数字为2
0.007来说对应的二进制数字如何得到呢?7/1000除以1/300得到2.1四舍五入为2,那么对应的数字为2
0.008来说对应的二进制数字如何得到呢?8/1000除以1/300得到2.4四舍五入为2,那么对应的数字为2
0.009来说对应的二进制数字如何得到呢?9/1000除以1/300得到2.7四舍五入为3,那么对应的数字为3
0.010来说对应的二进制数字如何得到呢?10/1000除以1/300得到3四舍五入为3,那么对应的数字为3
0.011来说对应的二进制数字如何得到呢?11/1000除以1/300得到3.3四舍五入为3,那么对应的数字为3
那么
DECLARE @t TABLE(date char(21))
INSERT @t SELECT '1900-1-1 00:00:00.000'
INSERT @t SELECT '1900-1-1 00:00:00.001'
INSERT @t SELECT '1900-1-1 00:00:00.002'
INSERT @t SELECT '1900-1-1 00:00:00.003'
INSERT @t SELECT '1900-1-1 00:00:00.004'
INSERT @t SELECT '1900-1-1 00:00:00.005'
INSERT @t SELECT '1900-1-1 00:00:00.006'
INSERT @t SELECT '1900-1-1 00:00:00.007'
INSERT @t SELECT '1900-1-1 00:00:00.008'
INSERT @t SELECT '1900-1-1 00:00:00.009'
SELECT date,转换后的日期=CAST(date as datetime) FROM @t结果为: date 转换后的日期
1900-1-1 00:00:00.000 1900-01-01 00:00:00.000
1900-1-1 00:00:00.001 1900-01-01 00:00:00.000
1900-1-1 00:00:00.002 1900-01-01 00:00:00.003
1900-1-1 00:00:00.003 1900-01-01 00:00:00.003
1900-1-1 00:00:00.004 1900-01-01 00:00:00.003
1900-1-1 00:00:00.005 1900-01-01 00:00:00.007
1900-1-1 00:00:00.006 1900-01-01 00:00:00.007
1900-1-1 00:00:00.007 1900-01-01 00:00:00.007
1900-1-1 00:00:00.008 1900-01-01 00:00:00.007
1900-1-1 00:00:00.009 1900-01-01 00:00:00.010
了解了上面的日期的表示方式后,0.000与0.001为什么表示成0.000就好理解了。
0.000与0.001使用0来表示,那么使用0乘以1/300后为0,所以转换后的日期为1900-01-01 00:00:00.000
0.002、0.003、0.004使用1来表示,那么使用1乘以1/300后为0.0033,进行四舍五入,所以转换后的日期为1900-01-01 00:00:00.003
0.005、0.006、0.007、0.008使用2来表示,那么使用2乘以1/300后为0.0066,进行四舍五入,所以转换后的日期为1900-01-01 00:00:00.007
0.009使用3表示,那么使用3乘以1/300后为0.010,进行四舍五入,所以转换后的日期为1900-01-01 00:00:00.010