How do I get the current week range in SQL based on a given date? Currently using the following query which provides an incorrect resultset.
Declare @dt date = '1994-08-27'
SELECT [start_of_week] = DATEADD(WEEK, DATEDIFF(WEEK, @dt, CURRENT_TIMESTAMP), @dt);
Please check the image for details of expected result set:
You could get the day of the week and use that to go back to the day you want (in this example Sunday).
In MySQL dialect that looks like:
SELECT DATE_SUB(NOW(),INTERVAL DAYOFWEEK(NOW())-1 DAY);
Sunday is day 1, so that is why the -1 is there. NOW() is just right now, so you can replace that with your own date easily.
To get the first day of current week, try this :
DECLARE @dt DATE = '1994-08-27'
SET @StartOfWeek = DATEADD(ww, DATEDIFF(ww, 0, @dt), 0)
Information : the first day of the week for 1994-08-27 is 1994-08-22.
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.