Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: Returning Monday of the week not working for Sundays

Tags:

date

sql

I want to input a date and have it return the Monday of that week, but at my company the week starts on Monday and ends on Sunday. I tried using SET DATEFIRST 1 to make Monday the first day of the week, but this didn't help me with Sundays.

code:

DECLARE @proddate datetime = '6/12/2016'
SET DATEFIRST 1; SELECT DATEADD(wk, DATEDIFF(wk,0,@proddate), 0) MondayOfWeek

returns:

6/9/2016 -> 6/6/2016
6/12/2016 -> 6/13/2016 (should be 6/6/2016)
like image 1000
whatwhatwhat Avatar asked Nov 20 '25 19:11

whatwhatwhat


1 Answers

Try this:

SELECT DATEADD(DAY, 1-DATEPART(WEEKDAY, @proddate), @proddate)
like image 191
Mack Avatar answered Nov 22 '25 08:11

Mack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!