Powered by Blogger.

Saturday, February 15, 2014

Get weekday name in sql server 2008



In this post we will discuss how to get the weekday name in sql server 2008. Also you can check my previous posts on:

- How to find the difference in retrieving data with and without using index in sql server 2008?

- Implicitely typed variables in C#.Net

- Single stored procedure to insert update and delete in sql server 2008

By using the DATEPART() function we can get the weekday name. The DATEPART() function is used to return a single part of a date or time, such as year, quarter, month, day, week, weekday, hour, minute, second, millisecond etc.

Here DATEPART() tooks two parameter like below:

DATEPART ( datepart , date )

For datepart parameter values check this msdn article.

date: valid date or datetime expression.

Below is the query that will return weekday name, like if today is friday it will return friday.

Begin
      Declare @week int
      Set @week=Datepart(dw,GetDate())
      Select case @week
      When 1 then 'Sunday'
      When 2 then 'Monday'
      When 3 then 'Tuesday'
      When 4 then 'Wednesday'
      When 5 then 'Thursday'
      When 6 then 'Friday'
      Else 'Saturday'
    End
End

Out put:






0 comments

Post a Comment