Very Simple Script which find Monday of the Current Week
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) MondayOfCurrentWeek
Reference : Pinal Dave (http://blog.SQLAuthority.com)
August 20, 2007 by pinaldave
Very Simple Script which find Monday of the Current Week
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) MondayOfCurrentWeek
Reference : Pinal Dave (http://blog.SQLAuthority.com)
Posted in Pinal Dave, SQL, SQL Authority, SQL DateTime, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQL Utility, T SQL, Technology | 28 Comments
Pinal Dave is a Pluralsight Developer Evangelist. He has authored 9 SQL Server database books and have written over 2500 articles on the database technology on his blog at a http://blog.sqlauthority.com. Along with 9+ years of hands on experience he holds a Masters of Science degree and a number of certifications, including MCTS, MCDBA and MCAD (.NET). His past work experiences include Technology Evangelist at Microsoft and Sr. Consultant at SolidQ. Prior to joining Microsoft he was awarded the Microsoft MVP award for three continuous years for his contribution in the community. Here is the list of the Pinal Dave's books.
Follow @pinaldave
Send +Pinal Dave
an email at pinal@sqlauthority.com
Nupur Dave loves technology simply because it makes life more convenient. She is devoted to technology because it touches our heart makes our daily lives easier. Among the many technological programs she uses and embraces Windows Live most because she can do lots of things with ease – from photo management to movies; business emails to personal social media connections.
Your articles are very helpful. Thanks for the public service.
Your solutions are extremely useful. Thanks a ton brother
Dear Pinal Dev…
I have never found a simpler scripts in date functions any where in the world … Hats offf….
simple, to-the-point answers – perfect every time.
Thanks a bunch! :)
Awesome! Perfect solution as always! Many thanks…..
This does not work. Here is an example:
DECLARE @TODAY DATETIME
SET @TODAY = ‘June 14, 2009′
SELECT
@TODAY AS TODAY,
LAST_MONDAY = DATEADD(wk, DATEDIFF(wk,0,@TODAY), 0)
:-)
I believe that is because Sunday is classed as the first day of the week.
Therefore when the date is a Sunday, the Monday of the current week is tomorow.
:)
How to find Monday of last week?
@cimnil
Find Monday of this week , and use DATEADD to subtract 7 days.
Perfect job. Thanks
“I believe that is because Sunday is classed as the first day of the week.”
If I set Monday to be the first day of the week it still doesn’t work.
SET DATEFIRST 1;
DECLARE @TODAY DATETIME ;
SET @TODAY = ‘June 14, 2009′ ;
SELECT
@TODAY AS TODAY,
LAST_MONDAY = DATEADD(wk, DATEDIFF(wk,0,@TODAY), 0)
returns:
TODAY LAST_MONDAY
2009-06-14 00:00:00.000 2009-06-15 00:00:00.000
Try this:
DATEADD(wk, DATEDIFF(d, 0, GETDATE()) / 7, 0).
This works because 1/1/1900 (i.e. ’0′) was a Monday so you are basically dividing the days since then by 7 days exactly.
FYI – you can also find the day of the week based on a Monday start date (i.e. Monday = 0, Tuesday = 1, etc.) by using “% 7″ instead of “/ 7″.
i would like to find Saturday of this week.. do u have any idea?
The statement RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))
doesn’t return the date of last monday.
I get the value for ,@CURRENTDATE = 11 03 2010, and the whole thing return 08 mar 2010, which is THIS monday, not the last one.
what if I want to run a script in any time and the result must return the date of last monday, last tuesday til last friday.
Any way to do it?
The statement RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))
doesn’t return the date of last monday.
I get the value for ,@CURRENTDATE = 11 03 2010, and the whole thing return 08 mar 2010, which is THIS monday, not the last one.
Any query can give me the date of last monday til last friday, this mean I need 5 days no matter what day I run the script.
Pinal Dave – Thanks SO much – I have seen your posts before but this one gave me exactly what I wanted. and so simple… a summary for the week beginning on monday was MUCH simpler with this small bit of code! you are the BEST!
nice piece of script. simple yet effective!
I tried to find Wednesday of the current week on the same lines, but was not successful. Can you pl. help.
For any given date, I need the Wednesday of that week.
Thanks in advance,
GS
Try this
select dateadd(week,datediff(week,0,getdate()),0)+2
Hi There,
Dave’s tit bit is perfect for defalut date caliculations. i.e. by defauls Sunday is considerded as first day of week in US.
if you want to consider monday as first day of the week you may use the below tit bit.
SET DATEFIRST 1
select (DateAdd(d, – (DatePart(dw,’2011-08-21′) -1), ’2011-08-21′))
Since you helped me out here, I’ll pass along what it was exactly you helped me with. This query builds a result set that lists all the weeks in the past year for a view or function:
WITH CTE_PastYearinWeeks
AS (
select
/* Thanks to SQL Authority for below. It gets the number of weeks between now and the beginning of (sql) time and adds it to the beginning of (sql) time, thus setting it to the first day of the week! */
DATEADD(wk, DATEDIFF(wk,0,GETDATE()-365), 0) as DateStart
UNION ALL
SELECT DATEADD(WK,1,DateStart)
FROM CTE_PastYearinWeeks
WHERE DATEADD(WK,1,DateStart) <= GETDATE()
–and DATEPART(YYYY,DateStart) <= DATEPART(YYYY,GETDATE())
)
select DATEPART(YYYY,DateStart) as YearNum
, DATEPART(WK,DateStart) as WeekNum
, SUBSTRING(CONVERT(varchar, DateStart, 107),1,6) + ' – ' + CONVERT(varchar, DATEADD(DD,6,DateStart), 107) as WeekTitle
from CTE_PastYearinWeeks
OPTION (MAXRECURSION 0);
how to find next week your birthday alert using sql
You ROCK !!!!!! A Big Thumbs Up
I need to find MondayOfCurrentWeek for last year
I need to find Sunday, three months ago.
Thanks! Worked fine.
Hi there,
i would like to find Saturday of this week.. do u have any idea?
select DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 5) — Saturday of the current week