Advertisement
kromm77

Range of datetime in a year

May 18th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.61 KB | None | 0 0
  1. with Temp
  2. AS
  3. (
  4.     SELECT n
  5.     FROM (VALUES(0), (1), (2), (3), (4), (5), (6), (7), (8), (9)) Nums(n)
  6. ), Numbers
  7. AS
  8. (
  9.     SELECT n = t2.n * 10 + t1.n + 0
  10.     FROM Temp t1
  11.     CROSS JOIN Temp t2
  12. ), FromToCTE
  13. AS
  14. (
  15.     SELECT  
  16.       DATEADD(month, 1 * n, '2016-01-01') FromDate,
  17.       DATEADD(day,-1,DATEADD(month, 1 *(n +1), '2016-01-01') ) ToDate
  18.     FROM numbers
  19.     WHERE (n + 1) <= 200
  20.  ), range_date
  21.  as
  22.  (
  23.  select CONVERT(VARCHAR(6), FromDate, 112) as MonthYear, FromDate,ToDate from FromToCTE
  24.  where FromToCTE.ToDate < SYSDATETIME()
  25.  )
  26.  select  * from range_date
  27.  order by FromDate asc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement