Skip to content

Instantly share code, notes, and snippets.

@viktor-evdokimov
Last active May 10, 2017 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viktor-evdokimov/d4cb8115e1e4cc65da418d53e213cd50 to your computer and use it in GitHub Desktop.
Save viktor-evdokimov/d4cb8115e1e4cc65da418d53e213cd50 to your computer and use it in GitHub Desktop.
recursive CTE to get next 10 days

unfortunately you can't fill in 2-3 years worth of days with CTE in sql server

;with recursive t(n) as ( SELECT now() n union all select n + '1 day' from t where n < '2033-01-01') select * from t limit 10;
-- also this works
;with recursive t(n) as ( SELECT now() n union all select n + '1 day' from t) select * from t limit 1000;
;with t as ( SELECT getdate() n union all select DATEADD(dd, 1, n) from t where n < '2033-01-01') select TOP 10 * from t;
-- interesting that this works:
;with t as ( SELECT getdate() n union all select DATEADD(dd, 1, n) from t ) select TOP 101 * from t;
-- but this not
;with t as ( SELECT getdate() n union all select DATEADD(dd, 1, n) from t ) select TOP 102 * from t;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment