Solved

# oracle date question- sunday to saturday - one week

Posted on 2012-09-17
Medium Priority
766 Views
select next_day(to_date('19-sep-2012','dd-mon-yyyy')-7, 'Sunday') day1,
next_day(to_date('19-sep-2012','dd-mon-yyyy'), 'Saturday') day2
from dual;

DAY1|DAY2
9/16/2012|9/22/2012
----------------------

select next_day(to_date('22-sep-2012','dd-mon-yyyy')-7, 'Sunday') day1,
next_day(to_date('22-sep-2012','dd-mon-yyyy'), 'Saturday') day2
from dual;

should get the same answer as above

DAY1|DAY2
9/16/2012|9/22/2012

But I get this

DAY1|DAY2
9/16/2012|9/29/2012

How do I fix my query? WHen the date is entered I need to get the week from sunday to saturday ( just one week ) for the given date.
0
Question by:anumoses
• 4
• 4

LVL 74

Accepted Solution

sdstuber earned 1000 total points
ID: 38406223
Sep 22 is a Saturday.  Why would you expect Next_day to return itself?  What do you expect it to return if it were Sunday or other day of the week?

To get Saturday's to return themselves, you can substract 1 from the input date

next_day(to_date('22-sep-2012','dd-mon-yyyy')-1, 'Saturday') day2
0

LVL 6

Author Comment

ID: 38406237
Our schedule is between sunday to saturday. So if they want to check hours scheduled it will be for the week. From the given date we have to get the sunday and saturday only for that week.
0

LVL 74

Expert Comment

ID: 38406241
Another option

trunc(  your_date   ,'d')  Sunday_date,
next_day(trunc( your_date ,'d'),'Saturday') Saturday_date

This is based on NLS settings that define Sunday to be start of week
0

LVL 6

Author Comment

ID: 38406273
Is there a way to get in my format? of given date?
0

LVL 6

Author Comment

ID: 38406279
My earlier code works fine until a saturday date is the given date. If the given date falls on a saturday then it goes 2 weeks. So any simple fix?
0

LVL 74

Expert Comment

ID: 38406280
you're not formatting your results above,   next_day returns a DATE type
as does TRUNC.

if you want the results formatted, put them in TO_CHAR

for example....

to_char(trunc(  your_date   ,'d') ,'yyyy-mm-dd')
0

LVL 74

Expert Comment

ID: 38406285
the simple fix is to use http:#a38406241

for whatever date you want.  No math (-7, -1, etc)  is needed.
0

LVL 6

Author Closing Comment

ID: 38406297
thanks
0

## Featured Post

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any othā¦
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.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
###### Suggested Courses
Course of the Month14 days, 1 hour left to enroll