Technical Article

Day of Week

,

To find the day of week, regardless of
-SET DATEFIRST
-SET DATEFORMAT
-SET LANGUAGE

The first one is based on Zellers Congruence.
The second one is a SQL server proprietary.

DECLARE @sDate char(8)
SET @sDate = '19650124' --Winston Churchills last day
SELECT CASE  CAST(DATEDIFF(DAY,'17530101',@sDate) AS int)
 -  ( CAST(DATEDIFF(DAY,'17530101',@sDate) AS int) / 7) * 7
WHEN 0 THEN 'Monday'
WHEN 1 THEN 'Tuesday'
WHEN 2 THEN 'Wednesday'
WHEN 3 THEN 'Thursday'
WHEN 4 THEN 'Friday'
WHEN 5 THEN 'Saturday'
WHEN 6 THEN 'Sunday'
ELSE NULL
END

SELECT CASE(datepart(dw, '19650124')+ @@datefirst)%7 
WHEN 2 THEN 'Monday'
WHEN 3 THEN 'Tuesday'
WHEN 4 THEN 'Wednesday'
WHEN 5 THEN 'Thursday'
WHEN 6 THEN 'Friday'
WHEN 0 THEN 'Saturday'
WHEN 1 THEN 'Sunday'
ELSE NULL
END

Rate

5 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (1)

You rated this post out of 5. Change rating