Sunday, 19 July 2015

@@DATEFIRST


It gives the current starting day of the week that our database thinks as the first day of  the week. This is very helpful when we use functions like DATEPART(...) to get the day of the week as an integer.

Return Type:

tinyint

For US English, it gives:
1 for Monday
2 for Tuesday
3 for Wednesday
4 for Thursday
5 for Friday
6 for Saturday &
7 for Sunday.

For Example (for US English):
DATEPART(dw, anyDate) (Let on anyDate is Sunday)
--It will give 1 --
DATEPART(dw, anyDate) (Let on anyDate is Wednesday)
--It will give 4 --

Its value depends on:
1. The current active session of SET DATEFIRST
For example:
SET DATEFIRST 5 ( means we are setting Friday as the first day of week.)
DATEPART(dw, anyDate) (Let on anyDate is Sunday)
--It will give 3 --

2. Current SQL LANGUAGE
For example:
SET LANGUAGE Italian
SELECT @@ DATEFIRST
--It will give 1--

SET LANGUAGE us_english
SELECT @@ DATEFIRST
--It will give 7--