2

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

1
  • You could put this query into a procedure as well if you are ever looking for a different look back number; say you only want to grab 10 weeks back, or 15 weeks back. Just set a parameter for weeklookback or something like that Commented Sep 16, 2014 at 20:13

2 Answers 2

3

This should be what you are looking for:

SELECT *
FROM TABLE_NAME
WHERE date_field >
    (SELECT DATEADD(Week, - 13, GETDATE()))
Sign up to request clarification or add additional context in comments.

Comments

1

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())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.