简体   繁体   中英

SQL Query, Return date results dbdate - 7 days from selected date

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.

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');

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM