SQ-what
SQ-what

Reputation: 49

Using Dates as a SQL Filter

I have a table of data that goes quite some time back, but I only want to results in my query for the last 13 weeks. There is a date column in that table.

I can use

SELECT DATEADD(Week, -13, GETDATE())

to get the date of 13 weeks back as a separate query - but I am having trouble linking that into the initial select to only return me the last 13 weeks of data. I have used that query as the data should refresh every day to go back 13 weeks to that date.

Is there any way I can do that?

Thanks in advance

Upvotes: 2

Views: 48

Answers (2)

Nick H.
Nick H.

Reputation: 1616

I'm a bit confused on what your issue is. You should be able to use the dateadd() in your where clause:

SELECT * 
FROM TABLE
WHERE DATECOLUMNTOCOMPARE >  DATEADD(WEEK,-13,GETDATE())

Upvotes: 1

Will
Will

Reputation: 2880

This should be what you are looking for:

SELECT *
FROM TABLE_NAME
WHERE date_field >
    (SELECT DATEADD(Week, - 13, GETDATE()))

Upvotes: 3

Related Questions