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.
| Author |
Topic |
|
kiranmurali
Yak Posting Veteran
55 Posts |
Posted - 2010-09-03 : 04:43:50
|
| Hi,can any one help me how to get the week start,weekend and week of a particular month.1. the week start should be on mondayand the week end is on sundayand previous months date should not be displayed in the first and last week of the month i have to populate the my master table like thisID MONTH YEAR WEEK WEEKSTART WEEKEND WK1 1 2010 1 28 3 12 1 2010 2 4 10 23 1 2010 3 11 17 34 1 2010 4 18 24 45 1 2010 5 25 31 56 2 2010 1 1 7 67 2 2010 2 8 14 78 2 2010 3 15 21 89 2 2010 4 22 28 910 3 2010 1 1 7 1011 3 2010 2 8 14 1112 3 2010 3 15 21 1213 3 2010 4 22 28 1314 4 2010 1 29 4 1415 4 2010 2 5 11 1516 4 2010 3 12 18 1617 4 2010 4 19 25 1718 4 2010 5 26 2 1819 5 2010 2 3 9 1920 5 2010 3 10 16 2021 5 2010 4 17 23 2122 5 2010 5 24 30 2223 6 2010 1 31 6 2324 6 2010 2 7 13 2425 6 2010 3 14 20 2526 6 2010 4 21 27 2627 7 2010 1 28 4 2728 7 2010 2 5 11 2829 7 2010 3 12 18 2930 7 2010 4 19 25 3031 7 2010 5 26 1 3132 8 2010 1 2 8 3233 8 2010 2 9 15 3334 8 2010 3 16 22 3435 8 2010 4 23 29 3536 9 2010 1 30 5 3637 9 2010 2 6 12 3738 9 2010 3 13 19 3839 9 2010 4 20 26 3940 10 2010 1 27 3 4041 10 2010 2 4 10 4142 10 2010 3 11 17 4243 10 2010 4 18 24 4344 10 2010 5 25 31 4445 11 2010 1 1 7 4546 11 2010 2 8 14 4647 11 2010 3 15 21 4748 11 2010 4 22 28 4849 12 2010 1 29 5 4950 12 2010 2 6 12 5051 12 2010 3 13 19 5152 12 2010 4 20 26 5253 12 2010 5 27 2 53Thanks in advancekiranmayee |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-09-03 : 05:11:48
|
[code]DECLARE @Year SMALLINT = 2010;WITH cteSource(WK, WeekStart, WeekEnd)AS ( SELECT Number + 1 AS WK, DATEADD(DAY, DATEDIFF(DAY, 0, DATEADD(YEAR, @Year - 1900, 0)) / 7 * 7, 7 * Number) AS WeekStart, DATEADD(DAY, DATEDIFF(DAY, 0, DATEADD(YEAR, @Year - 1900, 0)) / 7 * 7, 7 * Number + 6) AS WeekEnd FROM master.dbo.spt_values WHERE type = 'P' AND number BETWEEN 0 AND 52), cteCalendarAS ( SELECT WK AS [ID], CASE WK WHEN 1 THEN 1 ELSE DATEPART(MONTH, WeekStart) END AS [MONTH], CASE WK WHEN 1 THEN @Year ELSE DATEPART(YEAR, WeekStart) END AS [YEAR], DATEPART(DAY, WeekStart) AS [WEEKSTART], DATEPART(DAY, WeekEnd) AS [WEEKEND] FROM cteSource)SELECT [ID], [MONTH], [YEAR], ROW_NUMBER() OVER (PARTITION BY [MONTH] ORDER BY [ID]) AS [WEEK], [WEEKSTART], [WEEKEND], [ID] AS [WK]FROM cteCalendar[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
kiranmurali
Yak Posting Veteran
55 Posts |
Posted - 2010-09-03 : 06:59:48
|
| Hi,thanks a lot for your quick response.i have one more request.if we take the current month the week is number 1 startsfrom 6th 1 to 5 was in previous month week 5.my concern is that it should display 1st week starting from 1st.it should end with end date.Pls give me suggestions to achieve this.Thanks in advance.Kiranmayee |
 |
|
|
kiranmurali
Yak Posting Veteran
55 Posts |
Posted - 2010-09-06 : 03:09:35
|
| using the above table i have to display as below WEEKS30 to 5 SEP 20106 to 12 SEP 201013 to 19 SEP 201020 to 26 SEP 2010but the first week should be 1 to 5 not 30 to 5and the last week should be 27 to 30 can any one help me in solving this.Thanks in advanceKiranmayee |
 |
|
|
kiranmurali
Yak Posting Veteran
55 Posts |
Posted - 2010-09-08 : 02:29:00
|
| can any one help me to achieve this,its very urgent.Thanks in advance |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-09-08 : 03:01:48
|
Have you tried altering the query yourself?Perhaps maybe using another CASE statement? N 56°04'39.26"E 12°55'05.63" |
 |
|
|
kiranmurali
Yak Posting Veteran
55 Posts |
Posted - 2010-09-08 : 03:56:24
|
| yes, i tried in different ways.regards,Kiranmayee |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-09-08 : 04:02:17
|
Can you show us? N 56°04'39.26"E 12°55'05.63" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-09-08 : 04:04:12
|
quote: Originally posted by kiranmurali using the above table i have to display as below WEEKS30 to 5 SEP 20106 to 12 SEP 201013 to 19 SEP 201020 to 26 SEP 2010
This is NOT as requested originally.Now you say you want every month to ahve it's weeks start from the 1st and end with the latest day? N 56°04'39.26"E 12°55'05.63" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-09-08 : 04:13:27
|
[code]DECLARE @Year SMALLINT = 2010;WITH cteDays(theDate)AS ( SELECT DATEADD(YEAR, @Year - 1900, 0) AS theDate UNION ALL SELECT DATEADD(DAY, 1, theDate) FROM cteDays WHERE DATEPART(YEAR, DATEADD(DAY, 1, theDate)) = @Year)SELECT ROW_NUMBER() OVER (ORDER BY DATEPART(MONTH, theDate), 1 + (DATEPART(DAY, theDate) - 1) / 7) AS ID, DATEPART(MONTH, theDate) AS [MONTH], DATEPART(YEAR, theDate) AS [YEAR], 1 + (DATEPART(DAY, theDate) - 1) / 7 AS [WEEK], MIN(DATEPART(DAY, theDate)) AS [WEEKSTART], MAX(DATEPART(DAY, theDate)) AS [WEEKEND]FROM cteDaysGROUP BY DATEPART(YEAR, theDate), DATEPART(MONTH, theDate), 1 + (DATEPART(DAY, theDate) - 1) / 7OPTION (MAXRECURSION 366)[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
kiranmurali
Yak Posting Veteran
55 Posts |
Posted - 2010-09-08 : 08:43:21
|
| Thanks a lot its working fine. |
 |
|
|
|
|
|
|
|