SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Week numbers and week start dates


Week numbers and week start dates

Author
Message
balars_2000
balars_2000
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 31
Hi Folks,
Can you help me out with this?

I have a report that displays the week # and start of the week date and sums up revenue collected for the period.
For example, if I choose current year, Week # would start as 1 and start of the week should be 5/01 (first Monday of the year) and so on till current week which is 24 and week date is 08/06.

Thanks.
t.hitendra
t.hitendra
Mr or Mrs. 500
Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)

Group: General Forum Members
Points: 570 Visits: 405
Hi there,

I have tried to write a query for your problem with this sample table, tblPurchaseOrder - PurchaseOrderDate & GrandTotal

here it is-

select weekno,newdate,sum(grandtotal) as grandtotal from
(select datepart(wk,purchaseorderdate) as weekno
,dateadd(dd,0,datediff(dd,0,purchaseorderdate-(datepart(weekday,purchaseorderdate)-1)+1)) as newdate,grandtotal
from tblpurchaseorder where purchaseorderdate<=getdate()) as tbl group by weekno,newdate

Here I have subtracted 1 from (weekday,purchaseorderdate) to
make it start from monday (default for monday will be 2).

and the whole equation gives monday of that week.

please try it and tell me if that is what you need.
balars_2000
balars_2000
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 31
hi mate
thanks for ur response; appreciate it.
sorry if i wasnt clear, i need week numbers and week start dates for any given date range or a year. if it was a year, for example 2009, the output would be

week # week start date
1 05/01
2 12/01
3 19/01

and so on till current date. if it was for a date range for example between 19/01 and 2/02 (for 2009) then it should be
week # week start date
1 19/01
2 26/01
3 02/02

thanks again for ur time Smile
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223757 Visits: 42006
balars_2000 (6/9/2009)
hi mate
thanks for ur response; appreciate it.
sorry if i wasnt clear, i need week numbers and week start dates for any given date range or a year. if it was a year, for example 2009, the output would be

week # week start date
1 05/01
2 12/01
3 19/01

and so on till current date. if it was for a date range for example between 19/01 and 2/02 (for 2009) then it should be
week # week start date
1 19/01
2 26/01
3 02/02

thanks again for ur time Smile



What do you want if data is missing for a whole week during the year?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
balars_2000
balars_2000
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 31
I would display 0's to indicate there was no data during that particular week.

--Ta
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223757 Visits: 42006
balars_2000 (6/9/2009)
I would display 0's to indicate there was no data during that particular week.

--Ta


Then you need a complete list of dates to join against for any given year...

DECLARE @FirstDate DATETIME
 SELECT @FirstDate = '20090104'

 SELECT v.Number AS Week,
        DATEADD(wk, v.Number   ,@FirstDate) AS StartDate,
        DATEADD(wk, v.Number+1 ,@FirstDate) AS NextDate
   FROM Master.dbo.spt_Values v
  WHERE Type = 'P' 
    AND Number BETWEEN 0 AND 51


--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
balars_2000
balars_2000
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 31
thanks jeff.
balars_2000
balars_2000
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 31
hi hitendar
any comments?

cheers
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223757 Visits: 42006
balars_2000 (6/10/2009)
thanks jeff.


You bet. Thanks for the feedback. So, are you all set or do you have a remaining question?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
balars_2000
balars_2000
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 31
Jeff, I have a problem.
The query is working fine and listing all week numbers and dates.
However, I am interested in week day starting Mondays which is 05/01 for current year and subsequently 12/01, 19/01 etc..
Alternatively I should also be able to specify a date range and get the week start from there as well.

I have posted in detail in my previous messages.

Thanks for asking Smile

Cheers
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search