user2624315
user2624315

Reputation: 327

Need to find out the date from certain period and number of days

I need the result as given by date by user. Suppose if the user gives the date 2013-01-14 and total days = 2, i need to filter out

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

and it should not return the date of 12 and 13. ie 2013-01-13, 2013-01-12

Similarly if the total days = 3 then

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

How can i possibly get this.

Upvotes: 1

Views: 49

Answers (1)

Kaf
Kaf

Reputation: 33809

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)

Upvotes: 1

Related Questions