Get All Dates from Current Year in SQL

Get all the date of current year or particular year, below is the scrip to get the record from sql server.

  1. Declare @Year AS INT =DATENAME(YYYY, getdate()) ;  
  2. declare @FromDate datetime=DATEADD(yyyy, @Year - 1900, 0),@ToDate datetime=DATEADD(yyyy, @Year - 1900 + 1, -1);  
  3.   
  4. WITH Dates (DateNo) AS (  
  5. SELECT DATEADD(DAY, DATEDIFF(DAY, 0, @ToDate) - DATEDIFF(DAY, @FromDate, @ToDate), 0)  
  6. UNION ALL SELECT DATEADD(DAY, 1, DateNo)  
  7. FROM Dates  
  8. WHERE DATEADD(DAY, 1, DateNo) <=@ToDate)   
  9.   
  10. SELECT DateNo as [Date],DATENAME(month, DateNo) as [Month Name],DATENAME(DW, DateNo) as [Day Name]  
  11. FROM Dates   
  12.   
  13. option (maxrecursion 365);