SQL SERVER – SQL SERVER – UDF – Get the Day of the Week Function – Part 3

Datetime functions and stored procedures always interests me. Nanda Kumar has suggested modification to previous written article about SQL SERVER – SQL SERVER – UDF – Get the Day of the Week Function – Part 2. He has improved on UDF.

SQL SERVER - SQL SERVER - UDF - Get the Day of the Week Function - Part 3


CREATE FUNCTION dbo.udf_DayOfWeek(@dtDate DATETIME)
RETURNS VARCHAR(10)
AS
BEGIN
DECLARE @rtDayofWeek VARCHAR(10)
DECLARE @weekDay INT
----Adding @@DATEFIRST gives 1 for Sunday to 6 for Friday and 0 for Saturday, whatever the DATEFIRST setting is
SET @weekDay=((DATEPART(dw,@dtDate)+@@DATEFIRST)%7)
SELECT @rtDayofWeek = CASE @weekDay
WHEN 1 THEN 'Sunday'
WHEN 2 THEN 'Monday'
WHEN 3 THEN 'Tuesday'
WHEN 4 THEN 'Wednesday'
WHEN 5 THEN 'Thursday'
WHEN 6 THEN 'Friday'
WHEN 0 THEN 'Saturday'
END
RETURN (@rtDayofWeek)
END
GO
SELECT dbo.udf_dayofweek(GETDATE())

Test Any Function to Get the Day of the Week With SET DATEFIRST

Day of week code has one trap that catches almost everyone: the number from DATEPART(dw, ...) depends on the @@DATEFIRST setting. In US English, Sunday is day 1. With British English, or after SET DATEFIRST 1, Monday is day 1. The same date can return a different number on another server, or even in another session.

The function above adds @@DATEFIRST to cancel out that setting, which is the right idea. Before you use it in production, test it under more than one setting:

  • Run SET DATEFIRST 1 and call the function for seven days in a row.
  • Run SET DATEFIRST 7 and do the same.
  • Compare both lists with a calendar.

Watch for NULL in that test. The first version of this function subtracted 7 inside the brackets. In T-SQL, the % operator keeps the sign of the number on its left, so with SET DATEFIRST 1 the value dropped below zero for Monday to Friday, the remainder was negative and the CASE returned NULL. The code above now uses (DATEPART(dw, @dtDate) + @@DATEFIRST) % 7. It gives the same 0 to 6 values, with 1 for Sunday and 0 for Saturday, and it can never go below zero. Run the seven day test again after any change to confirm it.

Also remember that SET LANGUAGE changes @@DATEFIRST as a side effect. A login with a different default language can change the result without anyone ever running SET DATEFIRST.

If all you need is the name, DATENAME(weekday, @dtDate) returns it directly. The name comes back in the language of the session, which is fine for reports in one language but something to remember if your users work in several. Whichever way you choose, keep the test script next to the function, so the next change gets the same check.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.

SQL DateTime, SQL Function, SQL Scripts
Previous Post
Naming SQL Server Instances So People Understand Them
Next Post
Windows Settings That Affect SQL Server

Related Posts

2 Comments. Leave new

  • Brijendra Pandey
    June 26, 2008 6:03 pm

    Hi Prashant Pandey,

    How can we restore data from log files (.ldf) in sql 2005

    Thanks & Regards

    BRijendra Pandey

    Reply
  • Satish Vellanki
    June 29, 2016 4:48 pm

    DECLARE @week varchar(15)
    DECLARE @weekday int
    Set @weekday=(DATEPART(dw,GETDATE()))
    SELECT @week=Case @weekday WHEN 1 Then ‘SUNDAY’
    WHEN 2 Then ‘MONDAY’
    WHEN 3Then ‘TUESDAY’
    WHEN 4 Then ‘WEDNESDAY’
    WHEN 5 Then ‘THURSDAY’
    WHEN 6 Then ‘FRIDAY’
    ELSE ‘SATURDAY’
    END
    SELECT @week

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.