如何在SQL中更改datetime数据类型的日期格式? [英] How to change date format of datetime data type in SQL?

查看:977
本文介绍了如何在SQL中更改datetime数据类型的日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期格式 2017年3月1日10:24:48 在SQL中存储为 2017-03-01 10:24:48.000 其中我的'dd-mm-yyyy'格式已转换为'yyyy-mm -dd'格式。

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.

如何将列的日期格式更改为所需的格式?我的表已经包含数据。

How can I change the date format of the column to my desired format? My table already contains data.

推荐答案

SQL Server不存储 DateTime 以任何字符串格式-存储为 8字节数字(二进制)值。

SQL Server doesn't store a DateTime in any string format - it's stored as an 8 byte numerical (binary) value.

各种设置(语言,日期格式)仅影响在SQL Server Management Studio中如何显示 DateTime -或当您尝试将字符串转换为 DateTime <时如何解析它/ code>。

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支持多种格式-请参见有关CAST和CONVERT的MSDN联机丛书。其中大多数格式取决于您所拥有的设置-因此,这些设置可能有时会起作用-有时不起作用。

There are many formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT. Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not.

解决的方法是使用SQL Server支持的(略有适应性的) ISO-8601日期格式-该格式始终有效 -不管您使用哪种SQL Server语言和dateformat设置。

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.

ISO SQL Server支持-8601格式两种类型:


  • YYYYMMDD 仅用于日期(无时间部分);请在此处注意:不要破折号!,这非常重要! YYYY-MM-DD ,与SQL Server中的dateformat设置无关,并且不能在所有情况下都可以工作!

  • YYYYMMDD for just dates (no time portion); 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-DDTHH:MM:SS 用于日期和时间-请在此处注意:此格式具有破折号(但它们可以省略),并在 DATETIME 的日期和时间部分之间使用固定的 T 作为分隔符。

  • 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.

这对SQL Server 2000及更高版本有效。

This is valid for SQL Server 2000 and newer.

如果使用SQL Server 2008或更高版本以及 DATE 数据类型(仅 DATE -不是 DATETIME !),那么实际上您也可以使用 YYYY-MM-DD 格式,并且在SQL中的任何设置下也可以使用服务器。

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.

不要问我,为什么整个主题如此棘手,有些令人困惑-就是这样。但是使用 YYYYMMDD 格式,您可以使用SQL Server的任何版本以及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.

对于SQL Server 2008及更高版本,建议仅使用日期部分使用 DATE ,而 DATETIME2(n) 当您同时需要日期和时间时。如果可能,您应该尝试逐步淘汰 DATETIME 数据类型

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. You should try to start phasing out the DATETIME datatype if ever possible

这篇关于如何在SQL中更改datetime数据类型的日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆