1 --A. 测试 datetime 精度问题
2 DECLARE @t TABLE(date char(21))
3 INSERT @t SELECT '1900-1-1 00:00:00.000'
4 INSERT @t SELECT '1900-1-1 00:00:00.001'
5 INSERT @t SELECT '1900-1-1 00:00:00.009'
6 INSERT @t SELECT '1900-1-1 00:00:00.002'
7 INSERT @t SELECT '1900-1-1 00:00:00.003'
8 INSERT @t SELECT '1900-1-1 00:00:00.004'
9 INSERT @t SELECT '1900-1-1 00:00:00.005'
10 INSERT @t SELECT '1900-1-1 00:00:00.006'
11 INSERT @t SELECT '1900-1-1 00:00:00.007'
12 INSERT @t SELECT '1900-1-1 00:00:00.008'
13 SELECT date,转换后的日期=CAST(date as datetime) FROM @t
14
15 /*--结果
16
17 date 转换后的日期
18 --------------------- --------------------------
19 1900-1-1 00:00:00.000 1900-01-01 00:00:00.000
20 1900-1-1 00:00:00.001 1900-01-01 00:00:00.000
21 1900-1-1 00:00:00.009 1900-01-01 00:00:00.010
22 1900-1-1 00:00:00.002 1900-01-01 00:00:00.003
23 1900-1-1 00:00:00.003 1900-01-01 00:00:00.003
24 1900-1-1 00:00:00.004 1900-01-01 00:00:00.003
25 1900-1-1 00:00:00.005 1900-01-01 00:00:00.007
26 1900-1-1 00:00:00.006 1900-01-01 00:00:00.007
27 1900-1-1 00:00:00.007 1900-01-01 00:00:00.007
28 1900-1-1 00:00:00.008 1900-01-01 00:00:00.007
29
30 (所影响的行数为 10 行)
31 --*/
32 GO
33
34 --B. 对于 datetime 类型的纯日期和时间的十六进制表示
35 DECLARE @dt datetime
36
37 --单纯的日期
38 SET @dt='1900-1-2'
39 SELECT CAST(@dt as binary(8))
40 --结果: 0x0000000100000000
41
42 --单纯的时间
43 SET @dt='00:00:01'
44 SELECT CAST(@dt as binary(8))
45 --结果: 0x000000000000012C
46 GO
47
48 --C. 对于 smalldatetime 类型的纯日期和时间的十六进制表示
49 DECLARE @dt smalldatetime
50
51 --单纯的日期
52 SET @dt='1900-1-2'
53 SELECT CAST(@dt as binary(4))
54 --结果: 0x00010000
55
56 --单纯的时间
57 SET @dt='00:10'
58 SELECT CAST(@dt as binary(4))
59 --结果: 0x0000000A
2 DECLARE @t TABLE(date char(21))
3 INSERT @t SELECT '1900-1-1 00:00:00.000'
4 INSERT @t SELECT '1900-1-1 00:00:00.001'
5 INSERT @t SELECT '1900-1-1 00:00:00.009'
6 INSERT @t SELECT '1900-1-1 00:00:00.002'
7 INSERT @t SELECT '1900-1-1 00:00:00.003'
8 INSERT @t SELECT '1900-1-1 00:00:00.004'
9 INSERT @t SELECT '1900-1-1 00:00:00.005'
10 INSERT @t SELECT '1900-1-1 00:00:00.006'
11 INSERT @t SELECT '1900-1-1 00:00:00.007'
12 INSERT @t SELECT '1900-1-1 00:00:00.008'
13 SELECT date,转换后的日期=CAST(date as datetime) FROM @t
14
15 /*--结果
16
17 date 转换后的日期
18 --------------------- --------------------------
19 1900-1-1 00:00:00.000 1900-01-01 00:00:00.000
20 1900-1-1 00:00:00.001 1900-01-01 00:00:00.000
21 1900-1-1 00:00:00.009 1900-01-01 00:00:00.010
22 1900-1-1 00:00:00.002 1900-01-01 00:00:00.003
23 1900-1-1 00:00:00.003 1900-01-01 00:00:00.003
24 1900-1-1 00:00:00.004 1900-01-01 00:00:00.003
25 1900-1-1 00:00:00.005 1900-01-01 00:00:00.007
26 1900-1-1 00:00:00.006 1900-01-01 00:00:00.007
27 1900-1-1 00:00:00.007 1900-01-01 00:00:00.007
28 1900-1-1 00:00:00.008 1900-01-01 00:00:00.007
29
30 (所影响的行数为 10 行)
31 --*/
32 GO
33
34 --B. 对于 datetime 类型的纯日期和时间的十六进制表示
35 DECLARE @dt datetime
36
37 --单纯的日期
38 SET @dt='1900-1-2'
39 SELECT CAST(@dt as binary(8))
40 --结果: 0x0000000100000000
41
42 --单纯的时间
43 SET @dt='00:00:01'
44 SELECT CAST(@dt as binary(8))
45 --结果: 0x000000000000012C
46 GO
47
48 --C. 对于 smalldatetime 类型的纯日期和时间的十六进制表示
49 DECLARE @dt smalldatetime
50
51 --单纯的日期
52 SET @dt='1900-1-2'
53 SELECT CAST(@dt as binary(4))
54 --结果: 0x00010000
55
56 --单纯的时间
57 SET @dt='00:10'
58 SELECT CAST(@dt as binary(4))
59 --结果: 0x0000000A