简体   繁体   English

SQL 服务器 mm/dd/yy 或 yy/dd/mm 或其他格式的 T-SQL 日期数据类型格式?

[英]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/dd12/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.

相关问题 SQL Server:将日期格式为dddd mm / dd / yy的字符串转换为日期 - SQL Server : convert string with format dddd mm/dd/yy to date 在SQL Server中将日期格式更改为dd / mm / yyyy更改为dd mon yy - Date format change to dd/mm/yyyy to dd mon yy in sql server SQL将&#39;mm / dd / yy&#39;字符串转换为日期格式 - SQL Convert 'mm/dd/yy' string to date format mm / dd / yy至今 - mm/dd/yy TO DATE SQL日期格式转换? [dd.mm.yy到YYYY-MM-DD] - SQL date format convert? [dd.mm.yy to YYYY-MM-DD] 将 dd/mm/yy nvarchar(255) 转换为日期格式 dd/mm/yyyy 的 SQL 语句 - SQL statement to convert dd/mm/yy nvarchar(255) to Date format dd/mm/yyyy pyspark sql 将日期格式从 mm/dd/yy hh:mm 或 yyyy-mm-dd hh:mm:ss 转换为 yyyy-mm-dd hh:mm 格式 - pyspark sql convert date format from mm/dd/yy hh:mm or yyyy-mm-dd hh:mm:ss into yyyy-mm-dd hh:mm format 如何在SQL Server 2008 R2中将系统日期格式转换为dd / mm / yy? - How to convert the system date format to dd/mm/yy in SQL Server 2008 R2? 如何在SQL Server中获取日期格式(dd-MMM-yy HH:mm:ss)? - How to get date format (dd-MMM-yy HH:mm:ss) in SQL Server? 临时更改SQL查询输出MM / YY-&gt; MM / DD / YYYY中日期列的格式 - Temporarily change format of a date column in SQL Query output MM/YY -> MM/DD/YYYY
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM