A colleague just asked me if I knew a way to get the first day in a week in SQL Server. While I'm sure that there are many ways to get the first day (date) in a week here is a very simple way that in my opinion is quite elegant. The example assumes that you want to get the date of the first day in the week that GETDATE() is in. You could of course replace GETDATE() with a column of the datetime type.
SELECT DATEADD(ww, DATEDIFF(ww,0,GETDATE()), 0)
The same method can be used to extract only the date part from a datetime in T-SQL:
DATEADD(dd, DATEDIFF(dd,0,GETDATE()), 0)
PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.
This blog is built with EPiServer Community, EPiServer CMS, ASP.NET MVC and a bunch of other great products. The source code is available for download at the projects page, where you also can read more about this site and my other projects.
read more
Comments
Donna 2 years ago
I've been searching around the internet for a simple solution to get the first date of the week for a couple of hours. Thank you for this post, it was exactly what I needed!
Graham Charles 1 years ago
Useful, although for me this always returns a Monday -- and not the day set in SET DATEFIRST. I'm still puzzling over why...
GG 1 years ago
This gets me always the first Monday in a week, but not the first day of the week, nor the Monday of a week assuming Monday is the first day. For example, if I use a Sunday instead of getdate(), it'll return the following day, the Monday, not the Monday six days earlier.
Pete 1 years ago
Cool I prefer this to other methods that may modify the datefirst sql server property. This is much safer for me and can be used in functions where datefirst can't be set.
As with GG if you put Sunday it will return the following day. A lot of times you want the Monday before the date specified but it can be solved easily with a case statement like below:
select case
WHEN DATEADD(ww, DATEDIFF(ww,0,GetDate()), 0) > GetDate() THEN
DATEADD(ww, -1, DATEADD(ww, DATEDIFF(ww,0,GetDate()), 0))
ELSE
DATEADD(ww, DATEDIFF(ww,0,GetDate()), 0)
END
Pete 1 years ago
@Graham DATEDIFF doesn't respect the DATEFIRST property. In my opinion a good thing as I have seen things go wrong with date calculations when it comes into play.
Johan 1 years ago
The script uses the fact that 1900-01-01 was a Monday.
That is why the result always is a Monday.
If you want to use another day then Monday as start day of the week I would suggest using this script instead:
Verify what day is the first of the week by using:
And change it if needed by using:
Andy 1 years ago
i used this simple technique, returns sunday of the current week.
Declare @Sunday DateTime
Set @Sunday = (DateAdd(d, - (DatePart(dw,GetDate()) -1), GetDate()))
Ruwan 10 months ago
Thanks very much
Hamish 9 months ago
Awsome skills man, took me ages to find this but well worth it.!!
Cheers
MK 7 months ago
Nice code.
This is how I get Monday of that week:
select getdate() + 7-datepart(weekday, getdate())-5
Ryan 6 months ago
Thanks for sharing this! I have read other forum posts about this same functionality and they all suggested using variables. This allows me to look at a records date, and scale a report by the Year, Month, Week, and then days. Thanks!!!