我想永久更改 DATEFIRST 的值(该值用于像 DATEPART 这样的函数)。
如果我这样做:SET DATEFIRST 1
,该值将在执行期间保持不变,但在执行后返回默认值 - 在此处为7。
我已经遇到过这个问题,我知道它与登录国家有关,但我忘记了我需要更改哪个表和哪个属性。
我想永久更改 DATEFIRST 的值(该值用于像 DATEPART 这样的函数)。
如果我这样做:SET DATEFIRST 1
,该值将在执行期间保持不变,但在执行后返回默认值 - 在此处为7。
我已经遇到过这个问题,我知道它与登录国家有关,但我忘记了我需要更改哪个表和哪个属性。
USE [master]
GO
ALTER LOGIN [login] WITH DEFAULT_LANGUAGE = [you_language]
GO
declare
/*
Let's say for a regular, boring day of Monday
, regarldess of the datefirst value, I want it to return Monday as 1 (first day of week)
*/
@ForDate datetime = '20170320',
-- you can play with any value here
@SetDateFirstValue int = 5
--this is only for testing
set datefirst @SetDateFirstValue;
select
datepart(dw,@ForDate) as ActualReturnedDayOfWeek
, datepart(dw,dateadd(dd, @@DATEFIRST - 1,@ForDate)) As IWantItThisWay
P.S. : 也许你唯一需要用到这个的情况是在函数或约束条件(可能是计算列)中,其中不允许进行SET
操作。如果允许SET
,则按照需要设置并忘记它。