dcsimg

Beginning date of any week in a year

August 9, 2004



>>Script Language and Platform: SQL Server 2000
For a given year and week, this function will return the date of the first day of that week.

Usage:

select dbo.udf_mondayofaweek(1,2001)
--results
2001-01-01 00:00:00.000

select dbo.udf_mondayofaweek(22,2003)
--results
2003-05-26 00:00:00.000

Author: MAK


set datefirst 1
go
Create function dbo.udf_Mondayofaweek(
@week int,
@year varchar(4))
returns datetime
as
begin
declare @date varchar(10)
declare @firstdayofweek datetime
set @date='01/01/'+@year
set @date =convert(varchar(10),convert(datetime,@date) 
  - (datepart(dw,@date)-1),101)
set @firstdayofweek =dateadd(ww,@week-1,@date)
return @firstdayofweek 
end
go

Disclaimer: We hope that the information on these script pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, or fitness for a particular purpose... Disclaimer Continued


Back to Database Journal Home








The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers