25 March, 2012

Get All Date Between Two Date in SQL

Use a recursive query:


WITH RetAlldate AS (
  SELECT CAST('20 Jan 2012' AS DATETIME) AS EventDate
  UNION ALL
  SELECT DATEADD(ww, 1, EventDate)
    FROM RetAlldate s
   WHERE DATEADD(ww, 1, EventDate) <= CAST('01 Dec 2012' AS DATETIME))
SELECT *
  FROM RetAlldate

No comments:

Post a Comment