MS SQL Tips & Tricks

How to find first and last day of the week

To finst first and last day of the week You can use script bellow. First code treats Saturday as a first of the week

declare @cDateTime datetime;

set @cDateTime = GetDate()

select dateadd( week, 
		datediff( week, 0, @cDateTime ), 
		0 ) as BeginOfTheWeek
		 
select dateadd(d,
		-1,
		dateadd( week, 
		datediff( week, 0, @cDateTime )+1, 
		0 )
		) as EndOfTheWeek

The second sample assumes that week starts on Monday and ends on Saturday

declare @cDateTime datetime;
set @cDateTime = '2009-05-17'

select dateadd( week, 
		datediff( week, 0, dateadd(d,-1,@cDateTime) ), 
		0 ) as BeginOfTheWeek;
		 
select dateadd(d,
		-1,
		dateadd( week, 
		datediff( week, 0, dateadd(d,-1,@cDateTime) )+1, 
		0 )
		) as EndOfTheWeek
Download SQL script

Learn more tricks

Add Comment

Comments

28th July 2021

cialis vs viagra

icosype

13th August 2021

cialis online

icosype

31st August 2021

https://buypropeciaon.com/ - topical propecia

empaxyJax

16th September 2021

Comprar Levitra Farmacias Andorra

Priligy

20th September 2021

Plaquenil

anesque