Click here to Skip to main content
15,889,462 members
Home / Discussions / Database
   

Database

 
GeneralRe: how to get week number and startday of the week in sql server 2008 Pin
chakran30-Oct-09 1:09
chakran30-Oct-09 1:09 
GeneralRe: how to get week number and startday of the week in sql server 2008 Pin
Niladri_Biswas30-Oct-09 1:45
Niladri_Biswas30-Oct-09 1:45 
GeneralRe: how to get week number and startday of the week in sql server 2008 Pin
chakran30-Oct-09 2:20
chakran30-Oct-09 2:20 
GeneralRe: how to get week number and startday of the week in sql server 2008 Pin
Niladri_Biswas30-Oct-09 3:36
Niladri_Biswas30-Oct-09 3:36 
GeneralRe: how to get week number and startday of the week in sql server 2008 Pin
chakran30-Oct-09 4:06
chakran30-Oct-09 4:06 
GeneralRe: how to get week number and startday of the week in sql server 2008 Pin
Niladri_Biswas30-Oct-09 5:11
Niladri_Biswas30-Oct-09 5:11 
GeneralRe: how to get week number and startday of the week in sql server 2008 Pin
chakran1-Nov-09 21:05
chakran1-Nov-09 21:05 
AnswerRe: how to get week number and startday of the week in sql server 2008 [modified] Pin
Niladri_Biswas29-Oct-09 22:30
Niladri_Biswas29-Oct-09 22:30 
Try this .

declare @tbl table(bugdate date)
insert into @tbl
select '12/13/2008' union all
select '12/14/2008' union all
select '12/21/2008' union all
select '12/23/2008' union all
select '12/30/2008' union all
select '1/2/2009' union all
select '1/6/2009' union all
select '12/20/2009' union all
select '12/28/2009' union all
select '1/1/2010'

select	bugdate,
		case when Datepart(yy,bugdate) = 2009 then Datepart(wk,bugdate) + 53*1
		     when Datepart(yy,bugdate) = 2010 then Datepart(wk,bugdate) + 53*2
		else Datepart(wk,bugdate)
		end as weeknumber		
		,DATEPART(dw,bugdate) weekdays
		,DATENAME(dw, DATEPART(dw,bugdate)) weekdayname
		,DATEadd(day, (1- DATEPART(dw,bugdate)), bugdate) AS startweek
		,DATENAME(dw, DATEadd(day, (1- DATEPART(dw,bugdate)), bugdate)) startweekdayname
		  from @tbl


Output:

bugdate	weeknumber	weekdays	weekdayname	startweek	startweekdayname
2008-12-13	50	7	Monday	2008-12-07	Sunday
2008-12-14	51	1	Tuesday	2008-12-14	Sunday
2008-12-21	52	1	Tuesday	2008-12-21	Sunday
2008-12-23	52	3	Thursday	2008-12-21	Sunday
2008-12-30	53	3	Thursday	2008-12-28	Sunday
2009-01-02	54	6	Sunday	2008-12-28	Sunday
2009-01-06	55	3	Thursday	2009-01-04	Sunday
2009-12-20	105	1	Tuesday	2009-12-20	Sunday
2009-12-28	106	2	Wednesday	2009-12-27	Sunday
2010-01-01	107	6	Sunday	2009-12-27	Sunday


Depending on ur requirement, accept the columns.

Note that I have multiplied 53 * 1 for 2009 & 53*2 for 2010. Now why 53?<br />
Because if you want to find out the last week of the last day of a year it will be 53. Try this Select Datepart(wk,'12/31/2008') or Select Datepart(wk,'12/31/2009') etc. The output will be 53. I am considering 2008(here) as my starting point and henceforth nothing to add with the weeks for this year. Since year 2009 is 1 ahead so I am multiplying 53 with 1(which is though of no use) and adding that to every week of that year(2009). The same rule apply for 2010 with the difference that 53 will be multiplied with 2. If we follow this rule then we can generalized a formula 53 * n where n=0,1....<br />

Note- Follow what Mr.Mycroft said. It is better for ur learning.
Smile | :)

Niladri Biswas

modified on Friday, October 30, 2009 5:03 AM

Questiondatabase Pin
sydneylandscaper28-Oct-09 21:56
sydneylandscaper28-Oct-09 21:56 
AnswerRe: database Pin
WoutL28-Oct-09 22:48
WoutL28-Oct-09 22:48 
AnswerRe: database Pin
dan!sh 29-Oct-09 0:23
professional dan!sh 29-Oct-09 0:23 
AnswerRe: database Pin
Mycroft Holmes29-Oct-09 0:25
professionalMycroft Holmes29-Oct-09 0:25 
AnswerRe: database Pin
dxlee29-Oct-09 4:16
dxlee29-Oct-09 4:16 
AnswerRe: database Pin
dreamaway82029-Oct-09 5:46
dreamaway82029-Oct-09 5:46 
AnswerRe: database Pin
Eddy Vluggen29-Oct-09 9:29
professionalEddy Vluggen29-Oct-09 9:29 
QuestionThe Saga Continues Pin
Roger Wright28-Oct-09 21:45
professionalRoger Wright28-Oct-09 21:45 
QuestionHow to seperate records Pin
kKamel28-Oct-09 20:35
kKamel28-Oct-09 20:35 
AnswerRe: How to seperate records Pin
Mycroft Holmes29-Oct-09 0:14
professionalMycroft Holmes29-Oct-09 0:14 
GeneralRe: How to seperate records Pin
kKamel29-Oct-09 0:44
kKamel29-Oct-09 0:44 
GeneralRe: How to seperate records Pin
Mycroft Holmes29-Oct-09 2:01
professionalMycroft Holmes29-Oct-09 2:01 
QuestionRe: How to seperate records Pin
Eddy Vluggen29-Oct-09 12:03
professionalEddy Vluggen29-Oct-09 12:03 
Questionhow to add int Value Pin
getaccessyr28-Oct-09 4:04
getaccessyr28-Oct-09 4:04 
GeneralRe: how to add int Value Pin
Mycroft Holmes28-Oct-09 14:42
professionalMycroft Holmes28-Oct-09 14:42 
GeneralRe: how to add int Value Pin
getaccessyr28-Oct-09 22:10
getaccessyr28-Oct-09 22:10 
GeneralRe: how to add int Value Pin
Mycroft Holmes29-Oct-09 0:11
professionalMycroft Holmes29-Oct-09 0:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.