0

I'm not that good, when it comes to SQL. I'm trying to get data from the last three weeks. I can get the current week number, but really don't know how to subtract it so that it goes three weeks back and takes all data from that period.

This is my current code. I can make it work with a fixed number (42 in this example), but that's not what I want.

enter image description here

And this is what the output should be:

enter image description here

Eric Brandt
  • 7,886
  • 3
  • 18
  • 35

1 Answers1

0

Please, avoid pictures of code....

That said, if you're using SQL Server, I believe the function you're looking for is DATEPART. See if this does what you need:

SELECT *
FROM tblData
WHERE fldWeekNum BETWEEN DATEPART(WEEK, GETDATE())-3 AND DATEPART(WEEK, GETDATE())

EDIT:

Based on the comments below, let's try subtracting 3 weeks from the current date, then getting the DATEPART for WEEK from that date. This depends on how fldWeekNum is calculated, so it may need some additional tweaking based on that.

SELECT *
FROM tblData
WHERE fldWeekNum 
  BETWEEN 
    DATEPART(WEEK,DATEADD(WEEK, -3, GETDATE())) 
    AND 
    DATEPART(WEEK, GETDATE());
Eric Brandt
  • 7,886
  • 3
  • 18
  • 35