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
 General SQL Server Forums
 New to SQL Server Programming
 Get date from week number in a year

Author  Topic 

allan8964
Posting Yak Master

249 Posts

Posted - 2014-02-11 : 16:10:03
Hi there,

Can I get a date from a week number in a certain year, like I know it's week 7 in 2014 (I may know the month.)?
Thanks in advance.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2014-02-11 : 20:05:11
maybe this?

select dateadd(week, 7, '2014-01-01')

Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-11 : 23:28:44
is it week of year value you get or week for month? can you give an example?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2014-02-12 : 10:05:26
First of all thanks for replies.
The week number here I have is week in a year. I have values of year like 2014, the month like Feb, June, etc. From these 3 I need to know the date range starting from Sun to Sat. For example, we're in week 7 now and Feb. 9 - Feb. 15 is the date range I am looking for.
For TG's statement I noticed it gave the one more than it's expected. That's because 2014-01-01 is in week one so if we use dateadd() function then should subtract 1, like select dataadd(week, 6, '2014'). But this one only gives one date based 01-01, 2014-01-01 is on Wend so the statement gives the date of Wedn too.
Thanks again.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-02-12 : 17:32:57
Like this one?
http://weblogs.sqlteam.com/peterl/archive/2009/12/01/How-to-get-a-date-from-Year-week-and-weekday.aspx



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-13 : 07:49:57
do you mean this?

--SELECT * FROM RVNUTYP_EXCPTN


DECLARE @Year int,@WeekNo int= 7
SET @Year=2014

SELECT DATEADD(dd,DATEDIFF(dd,0,DATEADD(wk,@WeekNo-1,DATEADD(yy,@Year-1900,0)))/7 * 7-1,0),
DATEADD(dd,DATEDIFF(dd,0,DATEADD(wk,@WeekNo-1,DATEADD(yy,@Year-1900,0)))/7 * 7+5,0)



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2014-02-22 : 16:54:57
Gentlemen, sorry for not following up. But these are all what I need. Thank you SO much!!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-23 : 02:51:46
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -