Solved

Quick sql help

Posted on 2011-02-16
14
184 Views
Last Modified: 2012-05-11
Hi,
Please have a look at the sql below.
How can I get the week of the month in this sql?
I.e weeks as 1, 2 ,3 ,4 for every month may be in  another column

THanks
select model, cast(DatePart(year, date) as varchar(10)) + '/' + cast( DatePart(quarter, date) as varchar(10)) + '/' +  cast(DatePart(ww, date) as varchar(10)) as date1, DatePart(ww, date)  as week1, count(MAN) as count1
from table
where DatePart(year, date) >= 2010
group by model, cast(DatePart(year, date) as varchar(10)) + '/' + cast( DatePart(quarter, date)  as varchar(10))+ '/' +  cast(DatePart(ww, date) as varchar(10)) , DatePart(ww, date)

Open in new window

0
Comment
Question by:SameerMirza
  • 8
  • 3
  • 2
  • +1
14 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 34908644
what does this mean "week of the month"?
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34908647
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34908654
maybe this

DATEPART(DAY, GETDATE() - 1) / 7 + 1 AS theWeekWithinMonth
0
 
LVL 3

Expert Comment

by:QuinnDester
ID: 34908714
not sure if you want the week of the curent month or the number of the current week.

the code Hankurt gave you will give you the number of the week in the current month, this will give you the number of the current week

SELECT DATEPART( wk, GETDATE() )
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34908746
or maybe this

DatePart(ww, @date) - DATEPART(ww, dateadd(day, -datepart(day, @date)+1, @date))
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34908832
the one i posted above @ 34908746 gives you the diff between weeknumber of a date and the weeknumber of 1st day of that date

@date = 2011-02-16 -- current date
DatePart(ww, @date) = 8 -- week number of current date
dateadd(day, -datepart(day, @date)+1, @date) = 2011-02-01 -- first day of current date
DATEPART(ww, dateadd(day, -datepart(day, @date)+1, @date)) = 6 - week number of above

and the differnce is what you want I guess:

8-6 = 2

or maybe +1 is what you want

DatePart(ww, @date) - DATEPART(ww, dateadd(day, -datepart(day, @date)+1, @date)) +1


0
 

Author Comment

by:SameerMirza
ID: 34908885
I am lost...
how would tackle years in this one?
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 51

Expert Comment

by:HainKurt
ID: 34908926
ok, summary:

this is what you want

DatePart(ww, date) - DATEPART(ww, dateadd(day, -datepart(day, date)+1, date)) +1
or
DATEPART(DAY, date- 1) / 7 + 1

here is the implemenattion in your query:

select model, cast(DatePart(year, date) as varchar(10)) + '/' + cast( DatePart(quarter, date) as varchar(10)) + '/' +  cast(DatePart(ww, date) as varchar(10)) as date1, DatePart(ww, date)  as week1, count(MAN) as count1, DATEPART(DAY, date- 1) / 7 + 1 week_of_month
from table
where DatePart(year, date) >= 2010
group by model, cast(DatePart(year, date) as varchar(10)) + '/' + cast( DatePart(quarter, date)  as varchar(10))+ '/' +  cast(DatePart(ww, date) as varchar(10)) , DatePart(ww, date), DATEPART(DAY, date- 1) / 7 + 1

if this is not what you want, try the other option
0
 
LVL 2

Expert Comment

by:Eric3141
ID: 34908946
Try this.  I'm not at work so cannot verify but should be close.  







Declare
   @Date         datetime

-- Get first day of the week that Jan 1st falls in
Set @Date = select DATEADD(wk, DATEDIFF(wk,0,’01-Jan-2011’), 0);


While datepart(year, @Date) = ‘2011’
     BEGIN
	Select @Start_Date, datepart(week, @Date)

         -- add one week
         SET @Date = DateAdd(day, 7, @Date)
     END

Open in new window

0
 
LVL 2

Expert Comment

by:Eric3141
ID: 34908959
Sorry -- a typo above.  Use this instead.
Declare
   @Date         datetime

-- Get first day of the week that Jan 1st falls in
Set @Date = select DATEADD(wk, DATEDIFF(wk,0,’01-Jan-2011’), 0);


While datepart(year, @Date) = ‘2011’
     BEGIN
	Select @Date, datepart(week, @Date)

         -- add one week
         SET @Date = DateAdd(day, 7, @Date)
     END

Open in new window

0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 34908964
you can delete

"DatePart(ww, date)  as week1," from select and delete "DatePart(ww, date)," from group by

select model, cast(DatePart(year, date) as varchar(10)) + '/' + cast( DatePart(quarter, date) as varchar(10)) + '/' +  cast(DatePart(ww, date) as varchar(10)) as date1, count(MAN) as count1, DATEPART(DAY, date- 1) / 7 + 1 week_of_month
from table
where DatePart(year, date) >= 2010
group by model, cast(DatePart(year, date) as varchar(10)) + '/' + cast( DatePart(quarter, date)  as varchar(10))+ '/' +  cast(DatePart(ww, date) as varchar(10)) , DATEPART(DAY, date- 1) / 7 + 1

if you dont need that column at all after adding week_of_month
0
 

Author Comment

by:SameerMirza
ID: 34909147
First of all thanks alot for help. But may be I need a little more :)
DATEPART(DAY, date- 1) / 7 + 1 as week_of_month --- how does this work?

(7-1)/7+1 - 1.857 = 2 ----

when I see it against record. I see,

test      2010/1/2           14        1

according to my understanding it should have been showing

test      2010/1/2           14        3

Isnt it?
0
 

Author Closing Comment

by:SameerMirza
ID: 34909251
its kool. as I can see and work out what to add.
But may be you can post further scomments if you like.

Thanks
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34909570
Here I put all logic discussed here

I prefer this one because it looks more logical

DatePart(ww, @date) - DATEPART(ww, dateadd(day, -datepart(day, @date)+1, @date)) +1

check this query, wom3 is more accurate... if you can define what is the first week of a month, then we can write the qry... for now you can use wom3 :)

(change -5 to any number to test that day, +/- any number)
declare @date datetime = getdate()-5;

select @date date, 
DatePart(ww, @date) ww_date,
DATEPART(ww, dateadd(day, -datepart(day, @date)+1, @date)) ww_1stDayofmonth,
DATEPART(DAY, @date - 1) / 7 + 1 wom1,
DATEPART(DAY, @date) / 7 + 1 wom2,
DatePart(ww, @date) - DATEPART(ww, dateadd(day, -datepart(day, @date)+1, @date)) +1 wom3

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql calculate reminders 11 69
Pivot Query Problem 9 37
Selection from table2 where criteria for table1 10 31
Help with simplifying SQL 6 47
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now