最近一直在为datetime的事情挠头。关于用何种格式,用什么方式写入,精度等一堆乱七八糟的东西弄得我心情很糟。
最后我总结出一句话,有东西不会的时候先看说明书(对于sqlserver来说是msdn),如果那里没有在去其它地方找,很管用。
好了,不说废话了。说说结果。
对于日期时间,共有这些格式:
date:没说的,就是日期
time:就是时间
datetime:日期时间都有,时间精确到3(一会在告诉你什么叫精确到三)
smalldatetime:短日期时间,时间精确到分,秒的部分以00代替
datetime2:正主来了。
对于datetime2,他有一些格式控制。也就是说他的精度是可控的,
datetime2可以有从datetime2(0)到datetime2(7)八种不同的精度,其中datetime2(3)就是我刚才说的精确到3.
datetime2(0)是精确到秒的,其后每+1他的精度就多精确到秒后面一位。
附赠测试代码:
select CAST('2007-05-08 12:35:29. 1234567 +12:15' AS date) AS 'date' ,CAST('2007-05-08 12:35:29. 1234567 +12:15' AS time) AS 'time' ,CAST('2007-05-08 12:35:29. 1234567 +12:15' AS datetime) AS 'datetime' ,CAST('2007-05-08 12:35:29. 1234567 +12:15' AS smalldatetime) AS 'small' ,CAST('2007-05-08 12:35:29. 1234567 +12:15' AS datetime2(7)) AS 'datetime2-7' ,CAST('2007-05-08 12:35:29. 1234567 +12:15' AS datetime2(6)) AS 'datetime2-6' ,CAST('2007-05-08 12:35:29. 1234567 +12:15' AS datetime2(5)) AS 'datetime2-5' ,CAST('2007-05-08 12:35:29. 1234567 +12:15' AS datetime2(4)) AS 'datetime2-4' ,CAST('2007-05-08 12:35:29. 1234567 +12:15' AS datetime2(3)) AS 'datetime2-3' ,CAST('2007-05-08 12:35:29. 1234567 +12:15' AS datetime2(2)) AS 'datetime2-2' ,CAST('2007-05-08 12:35:29. 1234567 +12:15' AS datetime2(1)) AS 'datetime2-1' ,CAST('2007-05-08 12:35:29. 1234567 +12:15' AS datetime2(0)) AS 'datetime2-0'