14

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

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?

Thanks!


Update:

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

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.

Alexander Prokofyev
  • 33,874
  • 33
  • 95
  • 118
  • Sorry for not checking that the DateFirst was checked by datediff, who would have guessed, I've updated my answer to take this into account. – Tetraneutron Jul 09 '09 at 06:03

3 Answers3

22

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

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

Tetraneutron
  • 32,841
  • 3
  • 25
  • 21
  • 2
    SQL Server doesnt consider the **DATEFIRST** variable value indeed, from msdn docs: _Specifying SET DATEFIRST has no effect on DATEDIFF. DATEDIFF always uses Sunday as the first day of the week to ensure the function is deterministic._ Nice workaround! :) – rynkadink Oct 25 '13 at 10:23
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 become Saturday (which is 7nth day of week) and Mondфy(2) will first day of week

simply good
  • 991
  • 1
  • 12
  • 26
0

So if i'm getting this correctly, the only thing we need to do is remove 1 day from both dates on our datediff as following :

DATEDIFF(week,dateadd(day,-1,cast(GETDATE() as date)),
dateadd(day,-1,cast([Date] as date))) as RollingWeek 
Giomil83
  • 1
  • 1
  • Yes, that is correctly understood. DATEDIFF will always, use Sunday as the first day of the week to ensure the function operates in a deterministic way. By moving the dates 1 day back, you are "moving all Mondays to Sunday", making the result correct. e.g. If your week was to start with Wednesday you would have to move the dates 3 days back to "move all Wednesdays to Sunday" – LochNess Mar 26 '22 at 11:55