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
 Number of Weeks Until Date

Author  Topic 

xcrypted
Starting Member

2 Posts

Posted - 2012-09-17 : 19:58:23
Hi everyone,

I am having some trouble getting my sql statement to work. I need to find out how many weeks are left until December 20th of this year. Below is what I have, but, I just get errors.

select to_date('20-Dec-2012') - sysdate / 7 AS "Weeks Left"
from dual;

I know that select to_date('20-Dec-2012') - sysdate as "days" from dual;

gives me roughly 95 days. So my logic was to divide that number by 7 to give me the number of weeks.

Any help would be greatly appreciated.

Thanks!

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-17 : 20:09:38
This is a Microsoft SQL Server forum, so if you are using Oracle or another DBMS, a forum such as dbforums.com may be more appropriate. If you are using SQL Server, your code can be like this:
DECLARE @date DATETIME = '20121220';
SELECT DATEDIFF(DAY,GETDATE(),@date)/7.0 AS Weeks
There is also the concept of week boundaries, so if you care about ISO Weeks or any of that kind of stuff, this would not be what you need.
Go to Top of Page

singularity
Posting Yak Master

153 Posts

Posted - 2012-09-17 : 20:10:44
This is a SQL Server forum, and you're using Oracle. However, the solution is easy enough:


select (to_date('20-Dec-2012') - sysdate) / 7 AS "Weeks Left"
from dual;
Go to Top of Page

xcrypted
Starting Member

2 Posts

Posted - 2012-09-17 : 20:40:52
This is exactly what I was looking for. Thank you and sorry for posting this here.


quote:
Originally posted by singularity

This is a SQL Server forum, and you're using Oracle. However, the solution is easy enough:


select (to_date('20-Dec-2012') - sysdate) / 7 AS "Weeks Left"
from dual;


Go to Top of Page
   

- Advertisement -