Dan Cundy
Dan Cundy

Reputation: 2859

Return all records within given date

Question

How do I return all records from today until a given number of days in the future?

Example in pseudo code

Select every record from table 
where plandate = today and the next 14 days

What I have tried?

select *
from joblist
WHERE  plandate >= GETDATE() +14 

Upvotes: 1

Views: 42

Answers (2)

You can do something like this: (I'm omitting declares)

set @today = GETDATE()
set @endDate = DATEADD(DAY,15,GETDATE())

SELECT your_Records from TABLE
WHERE plandate >= @today
AND plandate < @endDate

You can also use the BETWEEN clause:

SELECT your_Records from TABLE
WHERE plandate BETWEEN @today AND @endDate

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460278

You can use DATEADD:

WHERE  plandate >= CAST(GETDATE() AS DATE)
AND    plandate < DATEADD(day,+15, CAST(GETDATE() AS DATE))

Note that you have the Date type only if you use at least SQL-Server 2008.

Upvotes: 1

Related Questions