Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 date function

Author  Topic 

wided
Posting Yak Master

218 Posts

Posted - 2011-03-31 : 04:38:38
hello

I have a table containing the following information:

Year, week number

I need to know the date of the first day and last day of each week of the year

thank you

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2011-03-31 : 06:55:08
CREATE TABLE #TempTable ( [YEAR] int, [week] int )

INSERT INTO #TempTable
SELECT 2011,
5
UNION ALL
SELECT 2011,
6
UNION ALL
SELECT 2011,
8
UNION ALL
SELECT 2012,
15

SELECT *
fROM #TempTable



SELECT [YEAR],
[WEEK],
DATEADD(dd, number,
CAST('01-01-' + CAST([year] AS CHAR(4)) AS DATETIME)) AS WeekFirstDayDate,
DATEADD(dd, number,
CAST('01-01-' + CAST([year] AS CHAR(4)) AS DATETIME)) + 6 AS WeekLastDayDate
FROM dbo.F_TABLE_NUMBER_RANGE(1, 365)
CROSS APPLY #TempTable
WHERE DATENAME(weekday,
DATEADD(dd, number,
CAST('01-01-' + CAST([YEAR] AS CHAR(4)) AS DATETIME))) = 'MONDAY'
AND DATEPART(WEEK,
DATEADD(dd, number,
CAST('01-01-' + CAST([year] AS CHAR(4)) AS DATETIME))) = [week]
ORDER BY number

/*
For function F_TABLE_NUMBER_RANGE please visit
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685

*/

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page
   

- Advertisement -