Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

TO_CHAR with dates to get week number issue, to get week of year

2741522Jan 21 2015 — edited Jan 22 2015

I am trying to build a report showing weekly sales data and running into an issue with Dates and TO_CHAR..

Using the date 9/29/2007, I expect the return from the following to be 4:

SELECT TO_CHAR('09/29/2007','W') FROM DUAL

However, it returns 5!!! Testing further I tried the next day, since it is a Sunday, it should be the start of the next week, 5..

SELECT TO_CHAR('09/30/2007','W') FROM DUAL.. I do get a 5..

I understand that the implementation of TO_CHAR with the 'W' parameter defaults a week start on what ever the first day of the month is, but I need a method that is more calendar like (Sunday is the start of a week, Saturday end of the week).

Anyone have a nifty solution?

for the above thread solution some expert answered as

select TO_DATE ('04-JAN-2015','DD-MM-YYYY'), to_char(TO_DATE ('04-JAN-2015','DD-MM-YYYY'), 'FMDAY') day,

       (case when to_char(trunc(TO_DATE ('04-JAN-2015','DD-MM-YYYY'), 'mm'), 'FMDAY') = 'SUNDAY' then to_number(to_char(TO_DATE ('04-JAN-2015','DD-MM-YYYY'), 'W'))

             else ceil((to_char(TO_DATE ('04-JAN-2015','DD-MM-YYYY'), 'dd') + 1 - to_char(next_day(trunc(TO_DATE ('04-JAN-2015','DD-MM-YYYY'), 'mm'), 'SUNDAY'), 'dd'))/7)

        end)+1 week_no

from dual

it determines week of month by considering Sunday to Saturday as one week and perfectly working however

how to determines week of year by considering Sunday to Saturday as one week ?

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 19 2015
Added on Jan 21 2015
17 comments
5,680 views