我有一个查询,我试图从一个日期范围或比起始日期早60天的交易表中提取数据。
这就是我的意思。
表格中的数据:
select *
from Transactions
Created TransactionID
12/1/16 1
12/5/16 2
1/1/15 3
3/1/16 4
4/1/16 5
4/2/16 6
4/3/16 7
我想做的是返回4/1到4/4之间的数据。或者比4/1早60天的任何东西。所以输出应该是:
12/1/16 1
12/5/16 2
1/1/16 3
4/1/16 5
4/2/16 6
4/3/16 7
因为它不超过60天,所以应该省略3/1/16。
我以为我最初可以使用DateDiff - 但这只会让开始日期超过60天。
Select *
From Transactions
where created between Datediff(dd,-60,@startdate) and @enddate
返回,我知道原因:
3/1/16 4
4/1/16 5
4/2/16 6
4/3/16 7
我该如何做到这一点?
答案 0 :(得分:3)
1)使用dateadd
并从@startdate中减去60天。
2)根据您的条件使用or
。
Select *
From Transactions
where created < dateadd(dd,-60,@startdate)
or created between '2016-04-01' and '2016-04-04'