SQL Server: Get First And Last Date With Week Name And Total Days in Current Month

To know current day, last day, and the week name and total days inthe current month in SQL Server use the below SQL Server statement.

code

  1. DECLARE @MONTH INT=3;  
  2. DECLARE @YEAR INT=2016;  
  3. DECLARE @FIRSTDATE DATE;  
  4. DECLARE @LASTDATE DATE;  
  5. DECLARE @TOTALDAYS INT  
  6. SET @LASTDATE=(SELECTDATEADD(DAY,-1,DATEADD(MONTH,@MONTH,DATEADD(YEAR,@YEAR-1900,0))))  
  7. SET @FIRSTDATE=(SELECTDATEADD(MONTH,@MONTH-1,DATEADD(YEAR,@YEAR-1900,0)))  
  8. SET @TOTALDAYS=(DATEDIFF(D,@FIRSTDATE,@LASTDATE))+1  
  9. SELECT @FIRSTDATE AS [FIRST DATE OF MONTH],  
  10. DATENAME(DW,@FIRSTDATE)AS [FIRST DATE OF MONTH WEEK NAME] ,  
  11. @LASTDATE AS [LAST DATE OF MONTH],  
  12. DATENAME(DW,@LASTDATE)AS[LAST DATE OF MONTH WEEK NAME],  
  13. @TOTALDAYS AS [TOTAL DAYS IN MONTH]  

 

Ebook Download
View all
SQL Jobs
Read by 0 people
Download Now!
Learn
View all