简体   繁体   English

如何在SQL中更改datetime数据类型的日期格式?

[英]How to change date format of datetime data type in SQL?

My date format of "03-01-2017 10:24:48" is getting stored in SQL as "2017-03-01 10:24:48.000" where my 'dd-mm-yyyy' format is getting converted to 'yyyy-mm-dd' format. 我的日期格式"03-01-2017 10:24:48"正在SQL中存储为"2017-03-01 10:24:48.000" ,其中我的'dd-mm-yyyy'格式已转换为'yyyy-mm-dd'格式。

How can I change the date format of the column to my desired format? 如何将列的日期格式更改为所需的格式? My table already contains data. 我的表已包含数据。

SQL Server doesn't store a DateTime in any string format - it's stored as an 8 byte numerical (binary) value. SQL Server不会以任何字符串格式存储DateTime它以8字节数字 (二进制)值存储。

The various settings (language, date format) only influence how the DateTime is shown to you in SQL Server Management Studio - or how it is parsed when you attempt to convert a string to a DateTime . 各种设置(语言,日期格式)仅影响SQL Server Management Studio中如何显示DateTime或尝试将字符串转换为DateTime时如何解析。

There are many formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT . SQL Server支持多种格式-请参阅CAST和CONVERT上MSDN联机丛书 Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not. 这些格式中的大多数取决于您拥有的设置-因此,这些设置可能有时会起作用-有时不起作用。

The way to solve this is to use the (slightly adapted) ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and dateformat settings. 解决此问题的方法是使用SQL Server支持的(略有调整) ISO-8601日期格式 -这种格式始终有效 -不管您使用的SQL Server语言和dateformat设置如何。

The ISO-8601 format is supported by SQL Server comes in two flavors: SQL Server支持ISO-8601格式 ,有两种形式:

  • YYYYMMDD for just dates (no time portion); YYYYMMDD仅显示日期(无时间部分); note here: no dashes! 注意: 没有破折号! , that's very important! ,那非常重要! YYYY-MM-DD is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations! YYYY-MM-DD 不是独立的在SQL Server中的日期格式设置,并不会在所有情况下工作!

or: 要么:

  • YYYY-MM-DDTHH:MM:SS for dates and times - note here: this format has dashes (but they can be omitted), and a fixed T as delimiter between the date and time portion of your DATETIME . YYYY-MM-DDTHH:MM:SS用于日期和时间-请在此处注意:此格式带有破折号(但可以省略),并且在DATETIME的日期和时间部分之间使用固定的T作为分隔符。

This is valid for SQL Server 2000 and newer. 这对SQL Server 2000及更高版本有效。

If you use SQL Server 2008 or newer and the DATE datatype (only DATE - not DATETIME !), then you can indeed also use the YYYY-MM-DD format and that will work, too, with any settings in your SQL Server. 如果您使用SQL Server 2008或更高版本以及DATE数据类型(仅DATE 不使用 DATETIME !),那么您实际上也可以使用YYYY-MM-DD格式,并且在SQL Server中的任何设置都可以使用。

Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. 不要问我为什么整个主题如此棘手且有些令人困惑-这就是事实。 But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server. 但是使用YYYYMMDD格式,则可以使用任何版本的SQL Server以及SQL Server中的任何语言和日期格式设置。

The recommendation for SQL Server 2008 and newer is to use DATE if you only need the date portion, and DATETIME2(n) when you need both date and time. 对于SQL Server 2008和更高版本,建议仅在需要日期部分的情况下使用DATE ,在需要日期和时间的情况下使用DATETIME2(n) You should try to start phasing out the DATETIME datatype if ever possible 如果可能,您应该尝试逐步淘汰DATETIME数据类型

Changing the format does not change the way the data is stored, just the way it is displayed. 更改格式不会更改数据的存储方式,而只会更改其显示方式。 You can find date and time styles at https://msdn.microsoft.com/en-us/library/ms187928.aspx?f=255&MSPPError=-2147217396 . 您可以在https://msdn.microsoft.com/zh-cn/library/ms187928.aspx?f=255&MSPPError=-2147217396中找到日期和时间样式。

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

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