Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Last Sunday of the Year

Posted on 2011-05-11
6
Medium Priority
?
446 Views
Last Modified: 2012-05-11
Hi,

Can someone tell me (in PL/SQL) how to retrieve the last sunday (for example) of the year.

Using Oracle 10g

Thanks,

Iain
0
Comment
Question by:mullykid
  • 3
  • 2
6 Comments
 
LVL 23

Assisted Solution

by:OP_Zaharin
OP_Zaharin earned 400 total points
ID: 35737324
- try this:

SELECT next_day((add_months(trunc(sysdate,'YEAR'), 12) - 1 )- 7,'SUN') FROM dual;

Open in new window

0
 
LVL 74

Accepted Solution

by:
sdstuber earned 600 total points
ID: 35737339
next_day(add_months(trunc(sysdate,'yyyy'),12)-8,'Sunday')
0
 
LVL 23

Expert Comment

by:OP_Zaharin
ID: 35737360
- to explain:
: to find last day of the year: (add_months(trunc(sysdate,'YEAR'),12) - 1)
: to find last Sunday of the week: next_day(sysdate - 7,'SUN')
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 74

Expert Comment

by:sdstuber
ID: 35737365
alternately

next_day(add_months(trunc(sysdate,'yyyy'),11)+23,'Sunday')
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 35737394
here it is (replace systdate with your variable on second line)
declare 
nextyear date := to_date(to_char(to_number(to_char(sysdate,'yyyy'))+1) || '0101', 'yyyymmdd');
lastSunday date;
begin

dbms_output.put_line(nextyear);
dbms_output.put_line(to_char(nextyear,'d'));
lastSunday  := nextyear - to_number(to_char(nextyear,'d'));
dbms_output.put_line(lastSunday);

end;

Open in new window

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35737420
I recommend not using date-to-string-to-number-to-string-to-date conversions in addition to date math.  Just use date math.

The "trick" is you want to find December 24th of the year.

Why?  Because that's one week from the end of the year.

Once you have the 24th,  NEXT_DAY for any day of the week will be the last of that day for the year.

So, you can either go to Jan 1st of the following year and backup 8 days
or you can go to Dec 1st of the current year and add 23 days

Either way you get to Dec 24 and then let next_day do its thing
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to recover a database from a user managed backup
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question