SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Get start and end date from week number
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

chiragvm
Yak Posting Veteran

India
63 Posts

Posted - 05/20/2013 :  04:09:40  Show Profile  Reply with Quote
Hi to all
i want to get start date and end date of the week from week number

suppose i am pass 20 in a function
function will return me a week start date and end date


-------------
Chirag
India
Sr. Sw.Engineer

Edited by - chiragvm on 05/20/2013 04:14:26

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 05/20/2013 :  04:16:46  Show Profile  Reply with Quote
See illustration below

DECLARE @WeekNo int= 4
DECLARE @Year int=2013

SELECT DATEADD(wk,@WeekNo-1,DATEADD(yy,@Year-1900,0)) AS WeekStart,
DATEADD(wk,@WeekNo,DATEADD(yy,@Year-1900,0))-1 AS WeekEnd



you can turn it into a UDF

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

chiragvm
Yak Posting Veteran

India
63 Posts

Posted - 05/20/2013 :  04:27:22  Show Profile  Reply with Quote
thanks visakh16 for quick answer
but it will give me tuesday to monday i want sunday to saturday

i am pass 20 and result is
WeekStart = 2013-05-14 00:00:00.000
WeekEnd = 2013-05-20 00:00:00.000

Result Should be
WeekStart = 2013-05-12 00:00:00.000
WeekEnd = 2013-05-18 23:59:59.000




-------------
Chirag
India
Sr. Sw.Engineer

Edited by - chiragvm on 05/20/2013 04:37:04
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 05/20/2013 :  05:07:15  Show Profile  Reply with Quote
--Try this

DECLARE @WeekNum INT
      , @YearNum char(4);

SELECT @WeekNum = 20 
     , @YearNum = 2013 
-- once you have the @WeekNum and @YearNum set, the following calculates the date range.
SELECT DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @YearNum) + (@WeekNum-1), 6) AS StartOfWeek;
SELECT DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @YearNum) + (@WeekNum-1), 5) AS EndOfWeek;


--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000