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.

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())

Reference : Pinal Dave (http://blog.SQLAuthority.com) , Nanda Kumar

About these ads

One thought on “SQL SERVER – SQL SERVER – UDF – Get the Day of the Week Function – Part 3

  1. Hi Prashant Pandey,

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

    Thanks & Regards

    BRijendra Pandey

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s