代码改变世界

SQL Server 2008 里面 DateTime2 解决过去恐怖的 MinValue问题

2011-01-20 13:18  mleader1  阅读(368)  评论(0编辑  收藏  举报

 

从 SQL Server 2008起,微软老大引入了一个新数据类型 datetime2, 允许 数据库中时间的最小值增大到与.NET Framework的日期格式一样精确,虽然还是有点不同,但是已经基本能够解决我们以前存取日期格式的问题:

 

     datetime2格式允许的时间范围是:"0001 / 01 / 01" through "9999 / 12 / 31"   即从c#中 DateTime.MinValue到 DateTime.MaxValue 的范围;

 

由于DateTime 为Null时返回值是按照 DateTime.MinValue来的,所以如果我们通过DateTime2来存储日期格式,就能够完美解决过去 "DateTime overflow " 的问题

 

 

同时MSDN也开始推荐用datetime2 来代替datetime - 既然微软老大也这么提倡了,那我们赶紧吧。。。

 

 

 

 

 

 

 

 

-----

The MSDN documentation for datetime recommends using datetime2 . Here is their recommendation:

Use the time, date, datetime2 and datetimeoffset data types for new work. These types align with the SQL Standard. They are more portable. time, datetime2 and datetimeoffset provide more seconds precision. datetimeoffset provides time zone support for globally deployed applications.

datetime2 has larger date range, a larger default fractional precision, and optional user-specified precision. Also depending on the user-specified precision it may use less storage.