Get Next Sunday With Different DATEFIRST Setting


  
 
 SET DATEFIRST 1; 
--SET DATEFIRST 7; 

Declare @TimeByDay Date
Set @TimeByDay = '2017-11-12'

--returns next Sunday in the week with any DATEFIRST value
Select   DATEADD(dd, 6-(((DATEPART(dw,@TimeByDay)+@@DATEFIRST) % 7)+5) % 7 , @TimeByDay) 

--not working  for  DATEFIRST<>1  
select DATEADD(dd,-(DATEPART(dw, @TimeByDay)-7), @TimeByDay) as WeekEndDate

--US default
SET DATEFIRST 7; 

 
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s