[英]T-SQL date data type format for SQL Server mm/dd/yy or yy/dd/mm or something else?
I am using this version of SQL Server我用的是这个版本的SQL Server
Microsoft SQL Server 2019 (RTM-GDR) (KB4517790) - 15.0.2070.41 (X64) Oct 28 2019 19:56:59 Copyright (C) 2019 Microsoft Corporation Developer Edition (64-bit) on Windows 10 Pro 10.0 <X64> (Build 19041: ) (Hypervisor)
information about locale setting有关语言环境设置的信息
select @@LANGUAGE;
-- result: us_english
I read reference document我阅读了参考文档
(source: https://learn.microsoft.com/en-us/sql/t-sql/data-types/date-transact-sql?view=sql-server-ver15 ) (来源: https://learn.microsoft.com/en-us/sql/t-sql/data-types/date-transact-sql?view=sql-server-ver15 )
I have我有
DECLARE @date DATE = '12/10/05';
DECLARE @datetime DATETIME = @date;
SELECT @date AS '@date', @datetime AS '@datetime';
As reference document point out, I think it is mm/[yy]yy/dd
( 12/10/05
; December 5th, 1910), but it return December 10th, 2005 .正如参考文件指出的那样,我认为它是mm/[yy]yy/dd
( 12/10/05
;1910 年 12 月 5 日),但它返回2005 年 12 月 10 日。
Is reference document wrong?参考文件错了吗? How to easy-to-understand these format?如何通俗易懂地理解这些格式? How to understand and remember?如何理解和记忆?
First thing is first - None of the Date/Time data types is stored with the display format in SQL Server.首先是第一件事 - SQL 服务器中没有任何日期/时间数据类型以显示格式存储。
Second, you've executed select @@LANGUAGE;
其次,你已经执行select @@LANGUAGE;
and got back us_english
- go on and run然后找回us_english
- go 并运行
SELECT dateformat
FROM sys.syslanguages
WHERE [Name] = @@LANGUAGE;
And you'll see the result of mdy
- which means that the formats for string representations of date values is one of the following:您将看到mdy
的结果——这意味着日期值的字符串表示格式是以下格式之一:
[m]m/dd/[yy]yy
[m]m-dd-[yy]yy
[m]m.dd.[yy]yy
Third, you should be aware of the fact that when dealing with SQL Server, the language settings are related to the default language of the login, which means different logins might have different language seetings, and of course, it is possible to change the language and / or the date format settings - which leads to the conclusion -第三,你要注意的是,在处理SQL服务器时,语言设置与登录的默认语言有关,也就是说不同的登录可能会有不同的语言设置,当然也可以更改语言和/或日期格式设置 - 得出结论 -
You should never rely on language settings for date/time string representations.您永远不应依赖日期/时间字符串表示的语言设置。 Instead, use one of formats specified in ISO 8601 , since these are the only two formats guaranteed to be interpreted correctly to a valid date/time value, regardless of the language and dateformat settings, and regardless of the actual data type, Even DateTime
And that's juse One reason why you should never use DateTime
again ).相反,使用ISO 8601中指定的格式之一,因为只有这两种格式可以保证被正确解释为有效的日期/时间值,无论语言和日期格式设置如何,也无论实际数据类型如何,甚至DateTime
和这就是为什么你不应该再次使用DateTime
的原因之一)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.