Birthday Report. [message #460142] |
Wed, 09 June 2010 20:24 |
meksta
Messages: 4 Registered: June 2010 Location: Sydney
|
Junior Member |
|
|
I need to create a report that shows only the DD-MON of a birthday while ignoring the year and then having the output in DATE form so I can sort chronologically. My input is in DD-MON-YYYY format.
I have used Date_Trunc to pull out the month and day but the 29-Feb dates are giving me the ORA-1839 error message.
|
|
|
|
Re: Birthday Report. [message #460160 is a reply to message #460150] |
Thu, 10 June 2010 00:07 |
meksta
Messages: 4 Registered: June 2010 Location: Sydney
|
Junior Member |
|
|
Sorry, I used the EUL_DATE_TRUNC function.
I tried using to_char but found that results were not in date form and hence were sorting numerically or alphabetically.
|
|
|
|
|
|
Re: Birthday Report. [message #460196 is a reply to message #460169] |
Thu, 10 June 2010 02:16 |
meksta
Messages: 4 Registered: June 2010 Location: Sydney
|
Junior Member |
|
|
Right So I have tried what you suggested
SQL> select to_char(sysdate, 'dd-mon') from dual;
And it brings the correct form. But I then need this colum sorted in order. When I sort it brings results
Currently
01-apr
01-aug
01-dec
05-apr
29-feb
Instead of
29-feb
01-apr
05-apr
01-aug
01-dec
Currently the table hold over 10,000 entries. How is it possible to have it sorted the way I want?
|
|
|
|
Re: Birthday Report. [message #460235 is a reply to message #460198] |
Thu, 10 June 2010 04:55 |
cookiemonster
Messages: 13958 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
The problem you've got is that for oracle to order a date as a date it needs a full date - day/month/year at least.
I'm guessing that you're extracting the month and day and then converting back to date to order.
That doesn't work with 29-Feb because if you don't specify a year oracle assumes the current year, and this year isn't a leap year.
I can think of two work arounds:
1) order by to_char(date, 'MMDD') - this'll sort as character but give the right result:
SQL> WITH DATA AS (SELECT to_date('01-apr-2001', 'DD-mon-yyyy') birthday FROM dual UNION
2 SELECT to_date('01-aug-2004', 'DD-mon-yyyy') FROM dual UNION
3 SELECT to_date('01-dec-2009', 'DD-mon-yyyy') FROM dual UNION
4 SELECT to_date('05-apr-2007', 'DD-mon-yyyy') FROM dual UNION
5 SELECT to_date('29-feb-2008', 'DD-mon-yyyy') FROM dual)
6 SELECT to_char(birthday, 'DDMON')
7 FROM DATA
8 ORDER BY to_char(birthday, 'MMDD');
TO_CH
-----
29FEB
01APR
05APR
01AUG
01DEC
SQL>
2: When converting back to a date put in a year that's a leap year (doesn't matter which one):
SQL> WITH DATA AS (SELECT to_date('01-apr-2001', 'DD-mon-yyyy') birthday FROM dual UNION
2 SELECT to_date('01-aug-2004', 'DD-mon-yyyy') FROM dual UNION
3 SELECT to_date('01-dec-2009', 'DD-mon-yyyy') FROM dual UNION
4 SELECT to_date('05-apr-2007', 'DD-mon-yyyy') FROM dual UNION
5 SELECT to_date('29-feb-2008', 'DD-mon-yyyy') FROM dual)
6 SELECT to_char(birthday, 'DDMON')
7 FROM DATA
8 ORDER BY to_date(to_char(birthday, 'DDMON')||'2008', 'DDMONYYYY');
TO_CH
-----
29FEB
01APR
05APR
01AUG
01DEC
SQL>
|
|
|
Re: Birthday Report. [message #460236 is a reply to message #460235] |
Thu, 10 June 2010 04:59 |
cookiemonster
Messages: 13958 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
And next time you've got a question post the full query and results as well as expected results.
It saves on us having to guess what the issue is and will get you an answer faster.
I suggest you have a read of the forum guide
|
|
|