Home > front end >  SQL Query, Return date results dbdate - 7 days from selected date
SQL Query, Return date results dbdate - 7 days from selected date

Time:12-07

I need to return results from a MYtable where the date selected, in this case 2021-12-06, is less than 7 days away from EndDate

SELECT EndDate 
FROM MYtable 
WHERE ('2021-12-06' BETWEEN EndDate AND DATEADD(day, - 7, EndDate)) 

The above code does not work/return results but gives you an idea of where I'm at currently, should i using a between method to get these results or is there a better way of doing it. Anyway any advice that can be given will be much appreciated.

CodePudding user response:

i think this gonna work for you DATEADD needs day parameter and using BETWEEN is better this way:

SELECT enddate 
FROM Mytable
WHERE enddate BETWEEN '2021-12-06' AND DATEADD(day,-7,'2021-12-06');

CodePudding user response:

SELECT EndDate 
FROM Mytable 
WHERE ('2021-12-06' BETWEEN DATEADD(day, - 7, EndDate) AND EndDate)
  • Related