Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Oracle, selecting Week ending date

Posted on 2013-11-06
11
Medium Priority
?
570 Views
Last Modified: 2013-11-18
to_char(to_date(xdate, 'yyyymmdd'), 'iw') 

Open in new window


The above gives me the week of the year,

Is there a way to give me that ending saturday?

lets say results of week 01, i would rather it be, instead of week 01, the first satuday of the year.

any date format would be fine
0
Comment
Question by:FutureDBA-
  • 2
  • 2
  • 2
  • +3
11 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 39629230
I think you need to use NEXT_DATE together with TRUNK:

select next_date(trunc(sysdate, 'year'), 'saturday') from dual;

Open in new window


Trunc will give you the first day of the year, and the NEXT_DATE will give you the first Saturday
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39629241
try this one

, ceil((trunc(xdate) - next_day(trunc(xdate,'yyyy')-1,'saturday'))/7)+1 AS week_number

from that last day of last year, find the next Saturday = first Saturday of this year
get the difference between that date and the xdate value  / 7
treat that result as an integer (using CEIL() )
add 1
= week numbers starting at first Saturday of the year


I prepared some tests on this a while back, see this:
http://sqlfiddle.com/#!4/233ec/17
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39629246
oops not week numbers, sorry.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 11

Expert Comment

by:HuaMinChen
ID: 39629567
Here is the way to show the relevant Saturday for one date given. You do not need to care about which week it is.
select to_date('20131003', 'yyyymmdd')+DECODE(substr(upper(TO_CHAR(TO_DATE('20131003', 'YYYYMMDD'),'day')),1,3),'SAT',0,'FRI',1,'THU',2,'WED',3,'TUE',4,'MON',5,6) from dual;

Open in new window

0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 39630135
Or, like this:

select next_day(to_date(to_number(to_char(to_date(xdate,'yyyymmdd'), 'iw') * 7),'ddd') -7,'Sat') "WeekEnding"
from [your_table]

Note: You want want to use "w" instead of "iw" depending on how you want to treat the first partial week of the year for years when Jan. 1 is not a Sunday.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39630404
the first post is almost correct,  it needs to subtract one day, otherwise, if January 1st is a Saturday, it'll return the 2nd Saturday of the year

also the correct function is next_day, not next_date

select next_day(trunc(sysdate, 'year')-1, 'saturday') from dual;

or, using your xdate string...

next_day(trunc(to_date(xdate, 'yyyymmdd'),'yyyy')-1,'Saturday')
0
 

Author Comment

by:FutureDBA-
ID: 39631808
this is great, but i need this to happen for all weeks of the year. i want to select all saturdays of the year,

above statement only gives me the first saturday, i may have not worded correctly what i was trying to do.

thanks
0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 39631817
The suggestion that I gave you earlier (ID: 39630135) will work for any date in your table.
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 39631825
yeah,  all Saturdays is definitely different than just one Saturday


    SELECT NEXT_DAY(TRUNC(SYSDATE, 'yyyy') - 1, 'Saturday') + ((LEVEL - 1) * 7)
      FROM DUAL
CONNECT BY TO_CHAR(NEXT_DAY(TRUNC(SYSDATE, 'yyyy') - 1, 'Saturday') + ((LEVEL - 1) * 7), 'yyyy') =
               TO_CHAR(SYSDATE, 'yyyy')

or, using your variable



    SELECT NEXT_DAY(TRUNC(to_date(xdate, 'yyyymmdd'), 'yyyy') - 1, 'Saturday') + ((LEVEL - 1) * 7)
      FROM DUAL
CONNECT BY TO_CHAR(NEXT_DAY(TRUNC(to_date(xdate, 'yyyymmdd'), 'yyyy') - 1, 'Saturday') + ((LEVEL - 1) * 7), 'yyyy') =
               TO_CHAR(to_date(xdate, 'yyyymmdd'), 'yyyy')
0
 
LVL 25

Expert Comment

by:chaau
ID: 39631837
It is not clear what do you mean. Do you want the SQL statement to return 52 (or sometimes 53) rows in a single select statement. It is possible to do using connect by prior, or using ANSI cte syntax
0
 
LVL 11

Expert Comment

by:HuaMinChen
ID: 39632535
Try this to list out all Saturdays
  SELECT *
  FROM (    SELECT TRUNC (TO_DATE ('20130101', 'YYYYMMDD'), 'YYYY') + LEVEL - 1
                      dt
              FROM DUAL
        CONNECT BY LEVEL <=
                        ADD_MONTHS (
                           TRUNC (TO_DATE ('20130101', 'YYYYMMDD'), 'YYYY'),
                           12)
                      - 1
                      - TRUNC (TO_DATE ('20130101', 'YYYYMMDD'), 'YYYY'))
WHERE TO_CHAR (dt, 'fmDAY') = 'SATURDAY';

Open in new window

0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

916 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