Thursday, March 10, 2011

DatePart, DATEFIRST, @@DateFirst in Sql Server.

The weekday  in sql server is represented as

Monday = 1
Tuesday = 2
Wednesday = 3
Sunday = 7

So, by default the Sunday(7) is set as the first day of the day.

So the query SELECT DATEPART(dw,'2011-03-09 20:28:36.090')

which falls on wednesday returns 4(Sunday-1.... Wednesday - 4)

you can set any day of the week to be the first day of the Week. Below is the syntax to do that

SET DATEFIRST 1 //Sets monday as the first day of the week
SELECT @@DATEFIRST   //return 1

SELECT DATEPART(dw,'2011-03-09 20:28:36.090'//return 3, since Monday is already set as default which is 1.

For more information on datepart

Hope it would have helpful to you..

No comments:

Post a Comment