简体   繁体   English

SQL Server DateTime2(0)与日期

[英]SQL Server DateTime2(0) vs Date

What are the implications of using SQL Server's DateTime2 with a precision of 0 to represent a date rather than the built in Date field. 使用SQL Server的DateTime2以精度0表示日期而不是内置日期字段有什么影响。

In either case, my concern is to prevent accidental time entries, but are there storage or performance considerations I should take note of? 在任何一种情况下,我担心的是防止意外的时间输入,但是我应该注意存储或性能方面的考虑吗?

DateTime2(0) will store datetime with no decimal values ie YYYY-MM-DD hh:mm:ss DateTime2(0)将存储没有小数值的日期时间,即YYYY-MM-DD hh:mm:ss

SELECT CONVERT(DateTime2(0) , GETDATE())
RESULT: 2015-04-06 20:47:17

Storing data just as dates will only store dates ie YYYY-MM-DD without any time values. 像日期一样存储数据只会存储日期,即没有任何时间值的YYYY-MM-DD

SELECT CONVERT(Date , GETDATE())
RESULT:  2015-04-06

If you are only interested in dates then use DATE data type. 如果您只对日期感兴趣,请使用DATE数据类型。

DATETIME2 will use 6 bytes for precisions less than 3 and DATE will use 3 bytes . DATETIME2将使用6 bytes用于less than 3精度, DATE将使用3 bytes

Date is half the size of DATETIME(0) hence it will also perform better since sql server will process less data and will save disk space as well. 日期是DATETIME(0)的一半大小因此它也将表现更好,因为sql server将处理更少的数据并且也将节省磁盘空间。

It won't work. 它不会起作用。 According to MSDN the minimum size of Datetime2 is six bytes and will contain hh:mm:ss so it can, and will, contain a time component (default of midnight). 根据MSDN,Datetime2的最小大小是六个字节,并且将包含hh:mm:ss,因此它可以包含时间组件(默认为午夜)。 As other responders have noted you must use a date type to guarantee that not time portion is saved and will occupy three bytes. 正如其他响应者所指出的那样,您必须使用日期类型来保证不保存时间部分并占用三个字节。

https://technet.microsoft.com/en-us/library/bb677335%28v=sql.105%29.aspx https://technet.microsoft.com/en-us/library/bb677335%28v=sql.105%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM