是否可以为T-SQL DATEDIFF函数设置星期开始?

时间:2022-03-17 01:39:29

I use DATEDIFF function to filter records added this week only:

我使用DATEDIFF函数来过滤本周添加的记录:

DATEDIFF(week, DateCreated, GETDATE()) = 0

and I noticed what it's assumed what week starts on Sunday. But in my case I would prefer to set start of week on Monday. Is it possible somehow in T-SQL?

而且我注意到星期天星期几开始的假设。但在我的情况下,我宁愿在星期一开始一周的开始。在T-SQL中有可能以某种方式吗?

Thanks!


Update:

Below is an example showing what DATEDIFF doesn't check @@DATEFIRST variable so I need another solution.

下面是一个示例,显示DATEDIFF没有检查@@ DATEFIRST变量,所以我需要另一个解决方案。

SET DATEFIRST 1;

SELECT 
    DateCreated, 
    DATEDIFF(week, DateCreated, CAST('20090725' AS DATETIME)) AS D25, 
    DATEDIFF(week, DateCreated, CAST('20090726' AS DATETIME)) AS D26
FROM
(
    SELECT CAST('20090724' AS DATETIME) AS DateCreated
    UNION 
    SELECT CAST('20090725' AS DATETIME) AS DateCreated
) AS T

Output:

DateCreated             D25         D26
----------------------- ----------- -----------
2009-07-24 00:00:00.000 0           1
2009-07-25 00:00:00.000 0           1

(2 row(s) affected)

26 Jul 2009 is Sunday, and I want DATEDIFF returns 0 in third column too.

2009年7月26日是星期天,我希望DATEDIFF也在第三栏中返回0。

2 个解决方案

#1


Yes it possible

是的可能

SET DATEFIRST 1; -- Monday

from http://msdn.microsoft.com/en-us/library/ms181598.aspx

It appears datediff doesn't respect the Datefirst, so make it do so run it like this

似乎dateiff不尊重Datefirst,所以让它这样做就像这样运行它

create table #testDates (id int identity(1,1), dateAdded datetime)
insert into #testDates values ('2009-07-09 15:41:39.510') -- thu
insert into #testDates values ('2009-07-06 15:41:39.510') -- mon
insert into #testDates values ('2009-07-05 15:41:39.510') -- sun
insert into #testDates values ('2009-07-04 15:41:39.510') -- sat

SET DATEFIRST 7 -- Sunday (Default
select * from #testdates where datediff(ww, DATEADD(dd,-@@datefirst,dateadded), DATEADD(dd,-@@datefirst,getdate())) = 0
SET DATEFIRST 1 -- Monday
select * from #testdates where datediff(ww, DATEADD(dd,-@@datefirst,dateadded), DATEADD(dd,-@@datefirst,getdate())) = 0

Stolen from

http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/8cc3493a-7ae5-4759-ab2a-e7683165320b

#2


I have another solution This should be easier to understand, correct me if I am wrong

我有另一个解决方案这应该更容易理解,如果我错了,请纠正我

SET DATEFIRST 1
select DATEDIFF(week, 0, DATEADD(day, -@@DATEFIRST, '2018-04-15 00:00:00.000'))

We subtract '-1' from date and Sunday will Saturday (which is 7nth day of week) and Mondey(2) will first day of week

我们从日期减去'-1',星期六到星期六(这是星期的第7天),Mondey(2)将从星期的第一天减去

#1


Yes it possible

是的可能

SET DATEFIRST 1; -- Monday

from http://msdn.microsoft.com/en-us/library/ms181598.aspx

It appears datediff doesn't respect the Datefirst, so make it do so run it like this

似乎dateiff不尊重Datefirst,所以让它这样做就像这样运行它

create table #testDates (id int identity(1,1), dateAdded datetime)
insert into #testDates values ('2009-07-09 15:41:39.510') -- thu
insert into #testDates values ('2009-07-06 15:41:39.510') -- mon
insert into #testDates values ('2009-07-05 15:41:39.510') -- sun
insert into #testDates values ('2009-07-04 15:41:39.510') -- sat

SET DATEFIRST 7 -- Sunday (Default
select * from #testdates where datediff(ww, DATEADD(dd,-@@datefirst,dateadded), DATEADD(dd,-@@datefirst,getdate())) = 0
SET DATEFIRST 1 -- Monday
select * from #testdates where datediff(ww, DATEADD(dd,-@@datefirst,dateadded), DATEADD(dd,-@@datefirst,getdate())) = 0

Stolen from

http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/8cc3493a-7ae5-4759-ab2a-e7683165320b

#2


I have another solution This should be easier to understand, correct me if I am wrong

我有另一个解决方案这应该更容易理解,如果我错了,请纠正我

SET DATEFIRST 1
select DATEDIFF(week, 0, DATEADD(day, -@@DATEFIRST, '2018-04-15 00:00:00.000'))

We subtract '-1' from date and Sunday will Saturday (which is 7nth day of week) and Mondey(2) will first day of week

我们从日期减去'-1',星期六到星期六(这是星期的第7天),Mondey(2)将从星期的第一天减去