I have written article about SQL SERVER – UDF – Get the Day of the Week Function. I have received good modified script from reader Mihir Popat has suggested another code where Sunday does not have to be necessary the first day of the week.
CREATE FUNCTION dbo.udf_DayOfWeek(@dtDate DATETIME)
RETURNS VARCHAR(10)
AS
BEGIN
DECLARE @rtDayofWeek VARCHAR(10)
DECLARE @weekDay INT
-- Here I have subtracted 7 For keeping Sunday as the First day
-- like wise for Monday we need to subtract 2 and so on
SET @weekDay = ((DATEPART(dw,GETDATE())+@@DATEFIRST-7)%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 7 THEN 'Saturday'
END
RETURN (@rtDayofWeek)
END
GO
SELECT dbo.udf_DayOfWeek(GETDATE())
Reference : Pinal Dave (http://blog.SQLAuthority.com), Mihir Popat
SQL SERVER – SQL SERVER – UDF – Get the Day of the Week Function – Part 2
May 23, 2008 by pinaldave
Posted in Pinal Dave, SQL, SQL Authority, SQL DateTime, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology | 5 Comments
5 Responses
Leave a Reply
-
About Pinal Dave
Pinalkumar Dave is Microsoft SQL Server MVP, Solid Quality Mentor and a prominent author of over 1000 SQL Server articles at SQLAuthority. He is a dynamic and proficient Principal Database Architect, Corporate Trainer and Project Manager specializing in SQL Server Programming with over 7 years of hands-on experience. He holds a degree in Masters of Science and has accomplished a number of certifications including MCDBA and MCAD (.NET). He has also been awarded Regional Mentor for PASS Asia.
-
Blog Stats
- 8,539,608 Readers
-
SQLAuthority Links

My Homepage
My Resume
My Other Blog
--------------------
Top Downloads
PDF Downloads
Script Downloads
Script Bank
Favorite Scripts
All Scripts - 1
All Scripts - 2
Top Articles
Best Articles
Favorite Articles - 1
Favorite Articles - 2
--------------------
SQL Interview Q & A
SQL Coding Standards
SQL FAQ Download
--------------------
Jobs @ SQLAuthority
Categories
- About Me (50)
- Best Practices (81)
- Business Intelligence (6)
- Data Warehousing (26)
- Database (258)
- DBA (121)
- DigiCorp (7)
- MVP (82)
- Poll (5)
- Readers Contribution (22)
- Readers Question (29)
- Software Development (62)
- SQL Add-On (87)
- SQL Backup and Restore (51)
- SQL BOL (8)
- SQL Coding Standards (20)
- SQL Constraint and Keys (48)
- SQL Cursor (29)
- SQL Data Storage (39)
- SQL DateTime (36)
- SQL Documentation (193)
- SQL Download (197)
- SQL Error Messages (117)
- SQL Function (106)
- SQL Humor (22)
- SQL Index (81)
- SQL Interview Questions and Answers (53)
- SQL Joins (60)
- SQL Optimization (51)
- SQL Performance (203)
- SQL Puzzle (18)
- SQL Security (114)
- SQL Server DBCC (41)
- SQL Server Management Studio (17)
- SQL Stored Procedure (96)
- SQL String (17)
- SQL System Table (28)
- SQL Trigger (26)
- SQL User Group (41)
- SQL Utility (116)
- SQL White Papers (8)
- SQLAuthority (309)
- SQLAuthority Author Visit (62)
- SQLAuthority Book Review (18)
- SQLAuthority News (280)
- SQLAuthority Website Review (23)
- SQLServer (44)
- Tech (783)
- Pinal Dave (773)
- SQL Scripts (491)
- Technology (1030)
- SQL (1030)
- SQL Authority (1030)
- SQL Query (1030)
- SQL Server (1030)
- SQL Tips and Tricks (1030)
- T SQL (1030)
-
Top Posts
- SQL SERVER - SELECT 1 vs SELECT * - An Interesting Observation
- SQL SERVER - Insert Data From One Table to Another Table - INSERT INTO SELECT - SELECT INTO TABLE
- SQL SERVER - Insert Multiple Records Using One Insert Statement - Use of UNION ALL
- SQL SERVER - Retrieve Current Date Time in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}
- SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server
- SQL SERVER - Convert Text to Numbers (Integer) - CAST and CONVERT
- SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects
- SQL Server Interview Questions and Answers Complete List Download
- SQL SERVER - 2008 - Interview Questions and Answers Complete List Download
- SQL SERVER - Restore Database Backup using SQL Script (T-SQL)
- SQL SERVER - 2005 List All Tables of Database
- SQL SERVER - Shrinking Truncate Log File - Log Full
-
Authors
-
pinaldave
- SQL SERVER – Four Different Ways to Find Recovery Model for Database
- SQL SERVER – Restore Sequence and Understanding NORECOVERY and RECOVERY
- SQL SERVER – Backup Timeline and Understanding of Database Restore Process in Full Recovery Model
- SQL SERVER – BLOB – Pointer to Image, Image in Database, FILESTREAM Storage
- SQLAuthority News – Big Thinkers – Robert Cain
- SQL SERVER – Standby Servers and Types of Standby Servers
- SQLAuthority News – Request SQLAuthority.com Stickers and SQL Server Cheat Sheet
- SQLAuthority News – Authors Visit – K-MUG TechEd Trivandrum on June 27, 2009
- SQLAuthority News – Book Review – Murach’s SQL Server 2008 for Developers
- SQLAuthority News – Authors Visit – DotNet Buzz Delhi TechEd Delhi on July 11, 2009
-
Archives
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
Pages
Category Cloud
Best Practices Database DBA MVP Pinal Dave Software Development SQL SQL Add-On SQL Authority SQLAuthority Author Visit SQLAuthority News SQL Backup and Restore SQL Documentation SQL Download SQL Error Messages SQL Function SQL Index SQL Interview Questions and Answers SQL Joins SQL Optimization SQL Performance SQL Query SQL Scripts SQL Security SQL Server SQL Stored Procedure SQL Tips and Tricks SQL Utility Technology T SQL



I’m still a little confused as to why you would want to go through the trouble of calling a UDF with 18 to 20 lines of code when the SQL Server development team has already done it for you.
SELECT DATENAME(dw, GETDATE())
I do understand the fun that can be had with trying to solve complex problems with T-SQL, but this is not one of those complex problems. It’s built in to the system for anyone to use when ever they want.
Hi Pinal,
I’m also surprised to see effort you’ve put to retrive Day of week for a given date. DATENAME function can perform this task pretty well.
I’m sure you must be aware of this function but you might have face some issue with it. Could you please share those issues wit DATENAME function?
Thanks & Regards,
Sudev Gandhi
[http://sudev.blogspot.com
Hi Pinal,
Actually the function u posted is returning ‘NULL’ for Saturdays and works for rest all days. And you didn’t use the given argument inside the funciton instead you used getdate(). this also returns error. The following is working…
Create FUNCTION dbo.udf_DayOfWeek(@dtDate DATETIME)
RETURNS VARCHAR(10)
AS
BEGIN
DECLARE @rtDayofWeek VARCHAR(10)
DECLARE @weekDay INT
–Here I have subtracted 7 For keeping Sunday as the First day like wise for Monday we need to subtract 2 and so on
set @weekDay=((DATEPART(dw,@dtDate)+@@DATEFIRST-7)%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())
I’m happy to reply this post as Novice DBA
Nandha
[...] 27, 2008 by pinaldave Datetime functions and stored procedures always interests me. Nanda Kumar has suggested modification to previous written article about SQL SERVER – SQL SERVER – UDF – Get [...]
Thanks a lot for this – right on the button and saved me a deal of work.
Great stuff
Ray
Sydney
Australia