SQL 如何将默认系统日期从 ymd 更改为 dmy
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6054794/
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
How to change default systemdate from ymd to dmy
提问by Rachid
Can somebody help me to change the default systemdate from ymd to dmy? These must be for always the default format !! How?
有人可以帮我将默认系统日期从 ymd 更改为 dmy 吗?这些必须始终是默认格式!如何?
回答by Mitch Wheat
Sets the order of the month, day, and year date parts for interpreting date, smalldatetime, datetime, datetime2 and datetimeoffset character strings.
设置用于解释 date、smalldatetime、datetime、datetime2 和 datetimeoffset 字符串的月、日和年日期部分的顺序。
[Note: This is often not the way to solve the problem of interpreting dates. Datetimes should not be stored a strings if you can avoid it (use a datetime or date column instead). If you have to store in a string form, use an ISO 8601 format which is basically of the form YYYYMMDD ]
[注意:这通常不是解决解释日期问题的方法。如果可以避免,则不应将日期时间存储为字符串(改为使用日期时间或日期列)。如果必须以字符串形式存储,请使用 ISO 8601 格式,该格式基本上为 YYYYMMDD ]
Example from MSDN:
来自 MSDN 的示例:
-- Set date format to day/month/year.
SET DATEFORMAT dmy;
GO
DECLARE @datevar datetime2 = '31/12/2008 09:01:01.1234567';
SELECT @datevar;
GO
-- Result: 2008-12-31 09:01:01.123
SET DATEFORMAT dmy;
GO
DECLARE @datevar datetime2 = '12/31/2008 09:01:01.1234567';
SELECT @datevar;
GO
-- Result: Msg 241: Conversion failed when converting date and/or time -- from character string.
GO
回答by Chris Fulstow
It's possible to set the default date format for the current login by changing the default language.
可以通过更改默认语言为当前登录设置默认日期格式。
For month/day/year:
对于月/日/年:
ALTER LOGIN [MyUser] WITH DEFAULT_LANGUAGE = us_english
select CAST('01-06-2011' as datetime)
-- 2011-01-06 00:00:00.000
Or for day/month/year:
或者对于日/月/年:
ALTER LOGIN [MyUser] WITH DEFAULT_LANGUAGE = British
select CAST('01-06-2011' as datetime)
-- 2011-06-01 00:00:00.000
You can choose from any of the languages listed in sys.syslanguages
. Changes won't take effect until you login again.
您可以从 中列出的任何语言中进行选择sys.syslanguages
。在您再次登录之前,更改不会生效。
回答by Gregory Meerable Lions
SET DATEFORMAT DMY;
GO
RECONFIGURE
GO
DBCC USEROPTIONS
SELECT GETDATE()