1

我需要用户按日期给出的结果。假设如果用户给出日期 2013-01-14 并且总天数 = 2,我需要过滤掉

2013-01-10
2013-01-11
2013-01-14
2013-01-15
2013-01-16

它不应该返回 12 和 13 的日期。即 2013-01-13, 2013-01-12

同样,如果总天数 = 3 那么

    2013-01-09
    2013-01-10
    2013-01-11
    2013-01-14
    2013-01-15
    2013-01-16
    2013-01-17

我怎么可能得到这个。

4

1 回答 1

1

Try this using DateAdd() function:

declare @Number int = 2, @Date Date = '20130114' --yyyymmdd format

select col1, col2
from yourTable
where dateCol >= dateAdd(day, -1*@Number, @Date) and dateCol< @Date

Update: Your description is not very clear I think what you need is to select records within +/- range from the given date.

select col1, col2
from yourTable
where dateCol >= dateAdd(day, -1*@Number, @Date) and dateCol <= dateAdd(day, @Number, @Date)
于 2013-10-22T19:53:22.687 回答