从上一天开始,获取最近14天(2周)的日期表

时间:2012-06-22 14:54:06

标签: sql sql-server tsql

我想写一个查询,其中列出了过去14天的DATES列 从昨天开始。

示例:

Dates 

2012-06-21
2012-06-20
2012-06-19
--
-

2 个答案:

答案 0 :(得分:4)

;WITH n(n) AS 
(
  SELECT TOP (14) ROW_NUMBER() OVER (ORDER BY [object_id]) 
  FROM sys.objects ORDER BY [object_id]
)
SELECT Dates = DATEADD(DAY, -n, DATEDIFF(DAY, 0, GETDATE())) FROM n
ORDER BY n;

答案 1 :(得分:0)

另一种方式;

;with days(day) as
(
    select getdate() - 1 as day
        union all
    select day - 1
        from days
        where day > dateadd(day, -14, getdate())
)
select cast(day as date) from days