document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. DECLARE @MONTH AS INT = 5 --Set the MONTH for which you want to generate the Calendar.
  2. DECLARE @YEAR AS INT = 2014 --Set the YEAR for which you want to generate the Calendar.
  3.  
  4.  
  5.  
  6. --Find and set the Start & End Date of the said Month-Year
  7. DECLARE @StartDate AS DATETIME = CONVERT(VARCHAR,@YEAR) + RIGHT(\'0\' + CONVERT(VARCHAR,@MONTH),2) + \'01\'
  8. DECLARE @EndDate AS DATETIME = DATEADD(DAY,-1,DATEADD(MONTH,1,@StartDate))
  9.  
  10.  
  11.  
  12. ;WITH Dates AS (
  13.   SELECT
  14.     @StartDate Dt
  15.   UNION ALL
  16.   SELECT
  17.     DATEADD(DAY,1,Dt)
  18.   FROM
  19.     Dates
  20.   WHERE
  21.     DATEADD(DAY,1,Dt) <= @EndDate
  22. ),Details AS (
  23.   SELECT
  24.     DAY(Dt) CDay,
  25.     DATEPART(WK,Dt) CWeek,
  26.     MONTH(Dt) CMonth,
  27.     YEAR(Dt) CYear,
  28.     DATENAME(WEEKDAY,Dt) DOW,
  29.     Dt
  30.   FROM
  31.     Dates
  32. )
  33.  
  34. --Selecting the Final Calendar
  35.  
  36. SELECT
  37.   Sunday,
  38.   Monday,
  39.   Tuesday,
  40.   Wednesday,
  41.   Thursday,
  42.   Friday,
  43.   Saturday
  44. FROM
  45.   (SELECT CWeek,DOW,CDay FROM Details) D
  46. PIVOT
  47. (
  48.   MIN(CDay)
  49.   FOR DOW IN (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday)
  50. ) AS PVT
  51. ORDER BY
  52.   CWeek
');