finding the start day (Monday) of the current week - sql-server

Looking for a SQL query/queries that would determine the start day (Monday) of the current week.
Example:
If today is -> then the start of the week is
Sat Oct 09, 2010 -> Start of the week is Monday Oct 04, 2010
Sun Oct 10, 2010 -> Start of the week is Monday Oct 04, 2010
Mon Oct 11, 2010 -> Start of the week is Monday Oct 11, 2010
Tue Oct 12, 2010 -> Start of the week is Monday Oct 11, 2010
I have seen many "solutions" on Google and StackOverflow. The look something like:
SET #pInputDate = CONVERT(VARCHAR(10), #pInputDate, 111)
SELECT DATEADD(DD, 1 - DATEPART(DW, #pInputDate), #pInputDate)
This fails because:
Sun Oct 10, 2010 -> start of week Monday Oct 11, 2010 (which is incorrect).

Try using DATEFIRST to explicitly set the day of week to be regarded as the 'first'.
set DATEFIRST 1 --Monday
select DATEADD(DD, 1 - DATEPART(DW, #pInputDate), #pInputDate)
This will return the Monday of the week the InputDate falls in.

Building on top of p.campbell's solution, if you don't want to use or can't use "SET DATEFIRST 1", you can get around that by doing the following:
SELECT DATEADD(DD, 2 - DATEPART(DW, DATEADD(DD, -1, #pInputDate)), DATEADD(DD, -1, #pInputDate))

The most simple implementation
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) MondayOfCurrentWeek

You don't need to use DATEFIRST:
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) -- Monday of current week
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()) - 1, 0) -- Monday of last week
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()) + 1, 0) -- Monday of next week
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) + 4 -- Friday of current week

You need to use DATEFIRST. Without it your code always assigns Sunday to "wrong" week:
SELECT DATEADD(wk, DATEDIFF(wk,0,'2020-10-11'), 0) --> Sunday -> 2020-10-12
SELECT DATEADD(wk, DATEDIFF(wk,0,'2020-10-12'), 0) --> Monday -> 2020-10-12
SELECT DATEADD(wk, DATEDIFF(wk,0,'2020-10-18'), 0) --> Sunday -> 2020-10-19
In this case 2 factors needs to be taken care of:
##DATEFIRST value
What day your weeks starts on (for me it is Monday)
E.g.: all below lines produce 2020-10-12 (Monday for week starting on Monday, 2020-10-12 and ending on Sunday, 2020-10-18) regardless of ##DATEFIRST value:
SELECT DATEADD(DAY, -((##DATEFIRST + 7 - (2 - DATEPART(WEEKDAY, '2020-10-12')) % 7) % 7), '2020-10-12')
SELECT DATEADD(DAY, -((##DATEFIRST + 7 - (2 - DATEPART(WEEKDAY, '2020-10-13')) % 7) % 7), '2020-10-13')
SELECT DATEADD(DAY, -((##DATEFIRST + 7 - (2 - DATEPART(WEEKDAY, '2020-10-14')) % 7) % 7), '2020-10-14')
SELECT DATEADD(DAY, -((##DATEFIRST + 7 - (2 - DATEPART(WEEKDAY, '2020-10-15')) % 7) % 7), '2020-10-15')
SELECT DATEADD(DAY, -((##DATEFIRST + 7 - (2 - DATEPART(WEEKDAY, '2020-10-16')) % 7) % 7), '2020-10-16')
SELECT DATEADD(DAY, -((##DATEFIRST + 7 - (2 - DATEPART(WEEKDAY, '2020-10-17')) % 7) % 7), '2020-10-17')
SELECT DATEADD(DAY, -((##DATEFIRST + 7 - (2 - DATEPART(WEEKDAY, '2020-10-18')) % 7) % 7), '2020-10-18')

Related

Obtaining the date of 2 Saturdays ago and Last Friday

How can I obtain the date of 2 Fridays ago and 2 Saturdays ago (SQL Server 2012+)
For example,
if run today, Monday Oct 12, my query should result in Sat Oct 3 and Friday Oct 9
if run on Tuesday Oct 20, my dates should be Sat Sat Oct 10 and Friday Oct 16
I am looking for an answer like select xyz getdate() ....
#Andresbi, your examples states 2 Saturdays before and 1 Friday before.
I think this code gives you the result:
declare #dt datetime = '20201020'
select
(
/* Previous sunday */
#dt - datepart(dw, #dt) + 1
/* Previous saturday */
- 1
/* and the saturday before */
- 7
),
(
/* Previous sunday */
#dt - datepart(dw, #dt) + 1
/* Previous friday */
- 2
)
select dateadd(day, -08, dateadd(wk, datediff(wk, 0, getdate()), 0))
select dateadd(day, -10, dateadd(wk, datediff(wk, 0, getdate()), 0))

How to handle DATEFIRST when using DATEPART

-- SET DATEFIRST to U.S. English default value of 7.
SET DATEFIRST 7;
SELECT
##DATEFIRST;
SELECT
GETDATE()
, DATEPART(dw , GETDATE()) AS DayOfWeek;
-- January 1, 1999 is a Friday. Because the U.S. English default
-- specifies Sunday as the first day of the week, DATEPART of 1999-1-1
-- (Friday) yields a value of 6, because Friday is the sixth day of the
-- week when you start with Sunday as day 1.
SET DATEFIRST 3;
SELECT
##DATEFIRST;
-- Because Wednesday is now considered the first day of the week,
-- DATEPART now shows that 1999-1-1 (a Friday) is the third day of the
-- week. The following DATEPART function should return a value of 3.
SELECT
GETDATE()
, DATEPART(dw , GETDATE()) AS DayOfWeek;
SET DATEFIRST 7;
How do we handle getting the DATEPART (1 = Sunday always) irregardless of DATEFIRST setting?
I really don't want to do a case and subtract...
this always seems ridiculous to me, how about
select datediff(day,0, getdate()) % 7
where 6 represents Sunday
or you could do
select (datediff(day,0, '2016-07-31') - 5) % 7
to get Sun = 1, Mon = 2, Tue = 3 ... etc
or you could do this fiddle
select (datepart(weekday,get_date()) + ##datefirst - 1) % 7 + 1
seems to work for all datepart
set datefirst 5
select (datepart(weekday,'2016-07-31') + ##datefirst - 1) % 7 + 1
select (datepart(weekday,'2016-08-01') + ##datefirst - 1) % 7 + 1
select (datepart(weekday,'2016-08-02') + ##datefirst - 1) % 7 + 1
select (datepart(weekday,'2016-08-03') + ##datefirst - 1) % 7 + 1
select (datepart(weekday,'2016-08-04') + ##datefirst - 1) % 7 + 1
select (datepart(weekday,'2016-08-05') + ##datefirst - 1) % 7 + 1
select (datepart(weekday,'2016-08-06') + ##datefirst - 1) % 7 + 1
select (datepart(weekday,'2016-08-07') + ##datefirst - 1) % 7 + 1
This code selects Monday as the first day of week whatever the setting in your engine:
((datepart(DW, #YourDateVariable) + ##DATEFIRST + 5) % 7) + 1
by adding ##DATEFIRST value before the modulus operator it neglects the datefirst setting in your SQL engine.
The value 5 is to make Monday as the first day of the week
To make Sunday as the first day of the week add 6
to make saturday as the first day of the week then add 0
and so on the rest of the day weeks.
Perhaps not the most elegant, but instead of doing the subtraction you can just set it to 7 and then back to what ever it was after your DATEPART
SET DATEFIRST 7;
SELECT DATEPART(dw , GETDATE()) --6
SET DATEFIRST 3;
DECLARE #currentDatefirst int = ##DATEFIRST
SELECT ##DATEFIRST --3
SET DATEFIRST 7;
SELECT DATEPART(dw , GETDATE()) --6
SET DATEFIRST #currentDatefirst
SELECT ##DATEFIRST --3

Add a number (13) to the month and change the day to the 1st of that month

I am sure this one is simple but I am having a difficult time figuring it out. I am trying to add 13 months to a date and that resulting month needs to default to the 1st day of the month.
Example:
Date: 1/25/2016
Query results: 2/1/2017
Here is the query I am using:
SELECT Dateadd(month,13,getdate())
This should work for you. Just replace GETDATE() with your date.
select dateadd(month, datediff(month, 0, dateadd(month, 13, GETDATE())), 0)
SQL 2012+
SELECT DATEADD(DAY, 1, EOMONTH(GETDATE(), 12))
You can just construct the date from the constituent parts, by adding 1 year + 1 month and forcing the day part to 1 (and taking care of the special case of month = 12), like this:
select DATEFROMPARTS (
year(getdate()) + case when month(getdate()) = 12 then 2 else 1 end,
case when month(getdate()) = 12 then 1 else month(getdate()) + 1 end,
1
)

Current week in current month showing wrong in SQL Server

This is how the July month looks like.
I am trying to pick the current week in current month with getdate() parameter using the below code.
declare #date datetime = getdate()
select datepart(day, datediff(day, 0, #date) / 7 * 7) / 7 + 1
Expecting the result as
Date -> week Number
2015-07-01 -> 1
2015-07-06 -> 2
2015-07-13 -> 3
But, instead, the result is:
Date -> week Number
2015-07-01 -> 5
2015-07-06 -> 1
2015-07-13 -> 2
How do I get the first format in SQL Server?
Thanks in advance
This seems to get the right answer
select datediff(week, dateadd(week, datediff(week, 0, dateadd(month, datediff(month, 0, #date), 0)), 0), #date)+1

Date Range of the x-th week in SQL Server?

I know the year and the index of the week, i.e. 2014, the 5th week. how can I find out the starting and ending date of this week in SQL Server? I do not really care whether the week starting with Monday or Sunday.
In mySQL, it has a MakeDate which may be able to do this. Is there an existing way to do this in SQL Server?
Thanks
Try this.
DECLARE #date DATE='2014-01-01'
SELECT Dateadd(dd, -( Datepart(dw, Dateadd(wk, 5, #date) - 1) ), Dateadd(wk, 5, #date)) [WeekStart],
Dateadd(dd, 7 - ( Datepart(dw, Dateadd(wk, 5, #date)) ), Dateadd(wk, 5, #date)) [WeekEnd]
SELECT
dateadd(wk,5,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)) as firstdayof5thweek2014,
dateadd(dd,6,dateadd(wk,5,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))) as lastdayof5thweek2014
SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) ==>gives you start day of the current year
SELECT dateadd(wk,5,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)) ==> On top add 5 weeks to get the start date of 5th week in 2014
SELECT dateadd(dd,6,dateadd(wk,5,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))) ==> On top add 6 days to get the last date of 5th week in 2014

Resources