Advertisement
SQLSoldier

List Months and Day Count Between Two Dates

Jun 2nd, 2016
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.49 KB | None | 0 0
  1. Declare @Date1 date,
  2.     @Date2 date;
  3.  
  4. Set @Date1 = '4/11/2015';
  5. Set @Date2 = getdate();
  6.  
  7. With Months
  8. As (Select Month(@Date1) As TheMonth,
  9.         Year(@Date1) As TheYear,
  10.         @Date1 As TheDate
  11.     Union All
  12.     Select Month(DateAdd(month, 1, TheDate)) As TheMonth,
  13.         Year(DateAdd(month, 1, TheDate)) As TheYear,
  14.         DateAdd(month, 1, TheDate) As TheDate
  15.     From Months
  16.     Where TheDate < @Date2)
  17. Select TheMonth, TheYear,
  18.     TheDays = DateDiff(day, EOMONTH(TheDate, -1), EOMONTH(TheDate))
  19. From Months;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement