In SQL Server, there’s a buildin function to calculate the number of weeks between two dates. This function is called “DateDiff”. The problem with this function is that Sql Server thinks the week starts on sunday. Maybe this it true in some situations but at  my current project,  the week should start on monday. This article gives you more info how to fix the issue with the datediff and weeks.

For example:

SQL
SELECT DATEDIFF(wk,'2012-01-07', '2012-01-08')

7th of January is saturday, 8th of January is sunday. The answer from SQL Server is 1. That’s true because the new week starts on sunday.

datediff_wk

Ok the problem is clear. How can I fix this problem? After doing some research, I found an article about Visual Basic. In VB, there’s a simular function to calculate with dates:

DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]])

Unfortunately, this last argument isn’t available in Sql Server. So looking around on the internet, I found this.

SQL
SET LANGUAGE Dutch

Specifies the language environment for the session. The session language determines the datetime formats and system messages.

After running this SET LANGUAGE command, I run the DateDiff command again:

SQL
SELECT DATEDIFF(wk,CAST('2012-01-07' AS DATETIME), CAST('2012-01-08' AS DATETIME) )

And the answer? 4. I don’t understand this. How can this be possible? I think because in Dutch, the date notation is YYYYDDMM so SQL server thinks that I mean July - August and then the answer is correct.

When I reverse the month and day:

SQL
SELECT DATEDIFF(wk,CAST('2012-07-01' AS DATETIME), CAST('2012-08-01' AS DATETIME) )

I get the answer: 1. Conclusion: this function will ignore the language setting  and always perform the calculation assuming Sunday as the first day of the week.

This is also interesting; when I cast an integer to datetime, the language setting will be ignored:

datadiff_2

There is a workaround for this issue. I found the function @@DATEFIRST. I never have used this one before. Try this:

SQL
SET LANGUAGE Dutch
SELECT @@DATEFIRST;
SET LANGUAGE us_english
SELECT @@DATEFIRST;

This function returns a number, the first day of the week. In the Netherlands, it is 1. This means monday. In US, it’s 7 and means sunday. So by knowing this, I can create a nice statement to do the calculation.

SQL
SET LANGUAGE Dutch;
SELECT DATEDIFF( week,
DATEADD(day, -@@DATEFIRST, '20091227'),
DATEADD(day, -@@DATEFIRST, '20091228') );

Answer: 1 = Correct.

Easy way: we know that @@DATEFIRST always returns 1 in my situation. So this also works:

SQL
SELECT DATEDIFF( week,
DATEADD(day, -1, '20120108'),
DATEADD(day, -1, '20120109') ) AS WeekDifference;

Friday to Saturday becomes Thursday to Friday: Difference = 0 weeks (before: 0 weeks) Saturday to Sunday becomes Friday to Saturday: Difference = 0 weeks (before: 1 week) Sunday to Monday becomes Saturday to Sunday: Difference = 1 weeks (before: 0 week)

Hope this helps you implementing the DateDiff function.

Frequently asked questions

Why does DATEDIFF week return 1 between a Saturday and a Sunday in SQL Server?

DATEDIFF assumes the week starts on Sunday. From 2012-01-07 (Saturday) to 2012-01-08 (Sunday) a new week starts, so the answer is 1, even though only one day has passed.

How do I make DATEDIFF count weeks that start on Monday?

Subtract one day from both dates before the call: DATEDIFF(week, DATEADD(day, -1, date1), DATEADD(day, -1, date2)). This works when @@DATEFIRST is 1, as in the Netherlands.

Does SET LANGUAGE change how DATEDIFF counts weeks?

No. After SET LANGUAGE Dutch, DATEDIFF with the wk interval still treats Sunday as the first day of the week. The function ignores the language setting.

What does @@DATEFIRST return in SQL Server?

It returns the number of the first day of the week. After SET LANGUAGE Dutch it is 1, which means Monday. After SET LANGUAGE us_english it is 7, which means Sunday.

Can I set the first day of the week in the DATEDIFF function?

No. Visual Basic has a firstdayofweek argument in its DateDiff, but SQL Server does not. Use the @@DATEFIRST workaround or shift both dates by one day.

1 comment

Your email address is not shown. A photo comes from Gravatar, if you have one.

  1. Rogier Pronk

    I think there is an easier solution for this...you can set the first day of the week:

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