How to get the day of the week ? I have been asked this question several times and we do have its solution via DATENAME function. However today we will achieve the same solution using FORMAT function, introduced in SQL Server 2012 as well. Given below are the two methods that help in identifying the day of a week in SQL Server 2005 & SQL Server 2012 respectively.
Method 1 :
Script supported in SQL Server 2005 and above :
--This script will work in SQL Server 2005 and above Declare @Datetime as datetime Set @Datetime =getdate() Select DATENAME(dw , @Datetime) as [Weekday Name] --OR Select DATENAME(weekday, @Datetime) as [Weekday Name] --OUTPUT
Method 2 :
Script supported in SQL Server 2012 and above :
--This script will work in SQL Server 2012 and above Declare @Datetime as datetime Set @Datetime =getdate() Select FORMAT(@Datetime,'dddd') as [Weekday Name] --OUTPUT
Have you come across any other method ? Do share.
Leave a Reply