Friday, July 27, 2007

UDF to Get Day of Week in SQL Server 2005

The following UDF can be used to get the Day of Week by passing it a specific date:

CREATE FUNCTION dbo.udf_Day_Of_Week(@p_Date DATETIME)
RETURNS VARCHAR(10)
AS
BEGIN
DECLARE @return_DayofWeek VARCHAR(10)
SELECT @return_DayofWeek = CASE DATEPART(dw,@p_Date)
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 (@return_DayofWeek)
END


1 comment:

Abdul Majid Tayyabi said...

Absoloutly fantastic... it worked..
i would be waiting for your comments on my blog http://learnsql2008.blogspot.com
.. hope you would like..