Technical Article

How to Find Beginning and End of Week

,

How to Find Beginning and End of Week
(for more scripts goto http://www.siccolo.com/articles.html)

drop function find_beg_of_week 
go

create function find_beg_of_week (@week_number int, @today datetime) returns datetime
as
begin
declare @start_week datetime

set @today = dateadd(wk, @week_number- datepart(wk,@today), @today)
set @start_week =  dateadd(d, 1 - DATEPART(dw, @today), @today) 

return @start_week 
end
go


drop function find_end_of_week 
go

create function find_end_of_week (@week_number int, @today datetime) returns datetime
as
begin
declare @end_week datetime
declare @start_week datetime

set @today = dateadd(wk, @week_number- datepart(wk,@today), @today)
set @start_week =  dateadd(d, 1 - DATEPART(dw, @today), @today) 
set @end_week = dateadd(d,6,@start_week )
return @end_week 
end
go


select dbo.find_beg_of_week(1,getdate()), dbo.find_end_of_week(1,getdate())

Rate

3 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

3 (2)

You rated this post out of 5. Change rating