SameerMirza
asked on
Quick sql help
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
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)
what does this mean "week of the month"?
maybe this
DATEPART(DAY, GETDATE() - 1) / 7 + 1 AS theWeekWithinMonth
DATEPART(DAY, GETDATE() - 1) / 7 + 1 AS theWeekWithinMonth
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() )
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() )
or maybe this
DatePart(ww, @date) - DATEPART(ww, dateadd(day, -datepart(day, @date)+1, @date))
DatePart(ww, @date) - DATEPART(ww, dateadd(day, -datepart(day, @date)+1, @date))
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
@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
ASKER
I am lost...
how would tackle years in this one?
how would tackle years in this one?
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
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
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
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
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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?
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?
ASKER
its kool. as I can see and work out what to add.
But may be you can post further scomments if you like.
Thanks
But may be you can post further scomments if you like.
Thanks
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)
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