Tuesday, July 5, 2016

SQL SERVER: Get first, last date and week day name of month

In this article I am going to explain how to get first, last date and week day name of month with number of days in Sql Server.


Description:
Recently I have got a requirement to get first and last date with week day name of month. I am going to share this in tutorial.

Implementation:

Run the below given Sql server query to Get Week day name and date of month.

Declare @CurrentDate datetime,@FirstDay datetime,@LastDay datetime, @totaldays int
Set @CurrentDate = getdate()
Set @FirstDay=(SELECT DATEADD(dd ,-(DAY(@CurrentDate )-1),@CurrentDate ))
Set @LastDay=(SELECT  DATEADD(dd ,-(DAY(@CurrentDate )), DATEADD(MONTH,1,@CurrentDate )))
set @totaldays = (datediff(dd,@FirstDay,@LastDay))+1

SELECT [Week Day on First Date]=datename(dw,@FirstDay)
,[First Date of Month] = @FirstDay
,[Week day on Last date]=datename(dw,@LastDay)
,[Last Date of Month]=@LastDay
,[Total Number of days]= @totaldays

 OUTPUT:
SQL SERVER: Get first and last date and week day name of month



No comments:

Post a Comment