daily pastebin goal
13%
SHARE
TWEET

List Months and Day Count Between Two Dates

SQLSoldier Jun 2nd, 2016 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top