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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Getting Month From Week Number

Author  Topic 

Deathnote
Starting Member

4 Posts

Posted - 2012-05-27 : 22:54:30
I hope someone who has a big heart as big as his brains would take a little out of his time to answer a noobs question.

I need to get week of the month by having week in a year as an input

example

201101 = JanW1(First week of january)

The formula to get 201101 format is this
DATEPART(YEAR, Molding_Date) * 100 + DATEPART(WEEK, Molding_Date)
Molding_Date Format= mm/dd/yyyy

Please reply if the question is confusing

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-27 : 23:51:10
[code]declare @year_wk int

select @year_wk = 201122

select Month01,
WeekOfMonth = @year_wk - (datepart(year, Month01) * 100 + datepart(week, dateadd(month, datediff(month, 0, Month01), 0))) + 1
from
(
select Month01 = dateadd(week, @year_wk % 100 - 1, dateadd(year, @year_wk / 100 - 1900, 0))
) d[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Deathnote
Starting Member

4 Posts

Posted - 2012-05-28 : 01:07:09
You are my god...Thank You khtan your the best :)
Go to Top of Page

Deathnote
Starting Member

4 Posts

Posted - 2012-05-29 : 01:52:06
Im sorry but i have further questions with the given code

you see

201218 = 5th week of april
201219 = 2nd week of May

Isnt 201219 suppose to be equal to 1st week of may?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-05-29 : 02:13:59
It depends.

1. What is a week for you?
2. When is first week of year?



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -