SQL TSQL 日期时间 ISO 8601
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/813238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TSQL DATETIME ISO 8601
提问by MartGriff
I have been given a specification that requires the ISO 8601
date format, does any one know the conversion codes or a way of getting these 2 examples:
我得到了一个需要ISO 8601
日期格式的规范,是否有人知道转换代码或获取这两个示例的方法:
ISO 8601 Extended Date 2000-01-14T13:42Z
ISO 8601 Basic Date 20090123T105321Z
回答by marc_s
When dealing with dates in SQL Server, the ISO-8601 format is probably the best way to go, since it just works regardless of your language and culture settings.
在 SQL Server 中处理日期时,ISO-8601 格式可能是最好的方法,因为无论您的语言和文化设置如何,它都可以正常工作。
In order to INSERT data into a SQL Server table, you don't need any conversion codes or anything at all - just specify your dates as literal strings
为了将数据插入 SQL Server 表,您不需要任何转换代码或任何东西 - 只需将日期指定为文字字符串
INSERT INTO MyTable(DateColumn) VALUES('20090430 12:34:56.790')
and you're done.
你就完成了。
If you need to convert a date column to ISO-8601 format on SELECT, you can use conversion code 126 or 127 (with timezone information) to achieve the ISO format.
如果需要在 SELECT 上将日期列转换为 ISO-8601 格式,可以使用转换代码 126 或 127(带时区信息)来实现 ISO 格式。
SELECT CONVERT(VARCHAR(33), DateColumn, 126) FROM MyTable
should give you:
应该给你:
2009-04-30T12:34:56.790
回答by Cyberherbalist
回答by John P
If you just need to output the date in ISO8601 format including the trailing Z and you are on at least SQL Server 2012, then you may use FORMAT
:
如果您只需要以 ISO8601 格式输出日期,包括尾随 Z 并且您至少使用 SQL Server 2012,那么您可以使用FORMAT
:
SELECT FORMAT(GetUtcDate(),'yyyy-MM-ddTHH:mm:ssZ')
This will give you something like:
这会给你类似的东西:
2016-02-18T21:34:14Z
Just as @Pxtl points out in a comment FORMAT may have performance implications, a cost that has to be considered compared to any flexibility it brings.
正如@Pxtl 在评论中指出的那样,FORMAT 可能会影响性能,与它带来的任何灵活性相比,必须考虑成本。
回答by Jeff Moden
Gosh, NO!!! You're asking for a world of hurt if you store formatted dates in SQL Server. Always store your dates and times and one of the SQL Server "date/time" datatypes (DATETIME, DATE, TIME, DATETIME2, whatever). Let the front end code resolve the method of display and only store formatted dates when you're building a staging table to build a file from. If you absolutely must display ISO date/time formats from SQL Server, only do it at display time. I can't emphasize enough... do NOT store formatted dates/times in SQL Server.
天哪,不!!!如果您在 SQL Server 中存储格式化的日期,那么您就会受到伤害。始终存储您的日期和时间以及 SQL Server“日期/时间”数据类型之一(DATETIME、DATE、TIME、DATETIME2,等等)。让前端代码解析显示方法,并在构建临时表以从中构建文件时仅存储格式化的日期。如果您绝对必须从 SQL Server 显示 ISO 日期/时间格式,请仅在显示时间执行。我怎么强调都不为过……不要在 SQL Server 中存储格式化的日期/时间。
{Edit}. The reasons for this are many but the most obvious are that, even with a nice ISO format (which is sortable), all future date calculations and searches (search for all rows in a given month, for example) will require at least an implicit conversion (which takes extra time) and if the stored formatted date isn't the format that you currently need, you'll need to first convert it to a date and then to the format you want.
{编辑}。造成这种情况的原因有很多,但最明显的是,即使使用良好的 ISO 格式(可排序),所有未来的日期计算和搜索(例如,搜索给定月份中的所有行)都至少需要一个隐式的转换(这需要额外的时间),如果存储的格式化日期不是您当前需要的格式,您需要先将其转换为日期,然后再转换为您想要的格式。
The same holds true for front end code. If you store a formatted date (which is text), it requires the same gyrations to display the local date format defined either by windows or the app.
这同样适用于前端代码。如果您存储格式化的日期(即文本),则需要相同的旋转来显示由 Windows 或应用程序定义的本地日期格式。
My recommendation is to always store the date/time as a DATETIME or other temporal datatype and only format the date at display time.
我的建议是始终将日期/时间存储为 DATETIME 或其他时间数据类型,并且仅在显示时间格式化日期。
回答by pimbrouwers
You technically have two options when speaking of ISO dates.
在谈到 ISO 日期时,从技术上讲,您有两种选择。
In general, if you're filtering specifically on Datevalues alone OR looking to persist date in a neutral fashion. Microsoft recommends using the language neutral format of ymd
or y-m-d
. Which are both valid ISO formats.
通常,如果您仅针对日期值进行专门过滤,或者希望以中性方式保留日期。微软建议使用的语言无关的格式ymd
或y-m-d
。两者都是有效的 ISO 格式。
Note that the form '2007-02-12' is considered language-neutral only for the data types DATE, DATETIME2, and DATETIMEOFFSET.
请注意,表单“2007-02-12”仅对于数据类型 DATE、DATETIME2 和 DATETIMEOFFSET 被视为与语言无关。
Because of this, your safest bet is to persist/filter based on the always neturalymd
format.
因此,您最安全的选择是基于始终网络ymd
格式进行持久化/过滤。
The code:
编码:
select convert(char(10), getdate(), 126) -- ISO YYYY-MM-DD
select convert(char(8), getdate(), 112) -- ISO YYYYMMDD (safest)