Friday, March 9, 2012

get the day from date value in SQL Server

Hello,

I want to know how can I (in sql server) get the day of week(eg Mon, Tue, Wed, etc) when a specific date is being given to the stored procedure. The day number will also do fine!!

In other words...I want to send a date to a stored procedure. Before anything in the sp can happen, I need to know the DayOfWeek the specified date falls on (day number also fine). Once I know the DayOfWeek I can continue with the rest of the work that actually needs to happen in the sp.

I know I can get the DayOfWeek in vb itself, but it would be easier if I can determine this in SQL Server and then just pass the date through as a paramater. If I have to get the DayOf Week in vb then I have to add a second paramater (one for DOW and one for the Date).

I hope the question is clear!?!

Give a look to books online to find the information you need. For a number to designate the day of the week, give a look to the DATEPART functon. For the name of the day of the week give a look to DATENAME. Hang on and I'll get you a couple of examples.

Here is a quick example. Note that DW is the designation for "Day of the Week:"

select datepart (dw, '5/11/7') as dayOfTheWeek,
datename (dw, '5/11/7') as NameOfDay

/*
dayOfTheWeek NameOfDay
6 Friday
*/

|||

Code Snippet

SELECT DATENAME (DW,GETDATE())

Gurpreet S. Gill

|||

Thanks guys!! I have been looking in the help files for SQl Server (2005), but I didn't spot it!!

Now that I have seen the solution, I remember that I have actually used it some time ago!!

No comments:

Post a Comment