How to sort date by month (JAN) and year(2009) [message #459355] |
Fri, 04 June 2010 08:42 |
rappaj
Messages: 147 Registered: June 2010 Location: NYC
|
Senior Member |
|
|
Hi,
I've done this once before, but can't seem to find the sql.
How can I sort by month and year on a column called ex: TEST_dATE
JAN 2007
FEB 2007
APR 2008
SEP 2009
OCT 2009
FEB 2010
JUN 2010
Thanks,
Joe
|
|
|
|
Re: How to sort date by month (JAN) and year(2009) [message #459363 is a reply to message #459356] |
Fri, 04 June 2010 09:15 |
rappaj
Messages: 147 Registered: June 2010 Location: NYC
|
Senior Member |
|
|
Hi Michel,
Sorry, I'm new to this forum and thanks for the quick reply.
Ok, here is the info requested. We are using Oracle Database 10g Enterprise Edition Release 10.2.0.2.0.
This would be the test scenerio we could use.
=================================
CREATE TABLE TEST_SORT
(START_DATE DATE)
insert into test_sort values ('22-JAN-2008')
insert into test_sort values ('01-MAR-2006')
insert into test_sort values ('15-DEC-2006')
insert into test_sort values ('10-DEC-2006')
insert into test_sort values ('30-APR-2010')
insert into test_sort values ('28-APR-2009')
insert into test_sort values ('28-APR-2007')
SELECT TO_CHAR(START_DATE, 'MON-YYYY') START_DATE FROM TEST_SORT
ORDER BY 1
START_DATE
==========
APR-2007
APR-2009
DEC-2006
DEC-2006
JAN-2008
MAR-2006
This obviously doesn't sort correctly.
RESULT SHOULD BE:
MAR-2006
DEC-2006
DEC-2006
APR-2007
JAN-2008
APR-2009
APR-2010
Thanks for your help...
Joe
[Updated on: Fri, 04 June 2010 09:22] Report message to a moderator
|
|
|
|
|
|
|
Re: How to sort date by month (JAN) and year(2009) [message #459374 is a reply to message #459371] |
Fri, 04 June 2010 09:32 |
rappaj
Messages: 147 Registered: June 2010 Location: NYC
|
Senior Member |
|
|
COOKIEMONSTER, BLACKSWAN and MICHEL....
Thanks for your help.
This works out perfect .
SELECT TO_CHAR(START_DATE, 'MON-YYYY') new_START_DATE FROM TEST_SORT
ORDER BY start_date;
I like this forum. I remember joining this, or something similar to FAQ) maybe 10+ years ago, but it was a little different back then. I'm keeping the link handy. You people are fast.
Thanks all.
|
|
|
|
|
|
|
|
|
|
Re: How to sort date by month (JAN) and year(2009) [message #459412 is a reply to message #459388] |
Fri, 04 June 2010 15:05 |
|
Barbara Boehmer
Messages: 9100 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
rappaj wrote on Fri, 04 June 2010 08:17
Actually, we chose blackswans SELECT TO_CHAR(START_DATE, 'YYYY-MM') START_DATE FROM TEST_SORT
ORDER BY 1
It sorted better that way.
If you did it right, the sort order is the same as demonstrated below. However, the second one allows usage of a different display format. If you got different results, I am guessing that you failed to supply a column alias that was different from the original column name, so that it ordered by the formatted column, instead of the original.
SCOTT@orcl_11g> SELECT TO_CHAR (start_date, 'YYYY-MM') start_date
2 FROM test_sort
3 ORDER BY 1
4 /
START_D
-------
2006-03
2006-12
2006-12
2007-04
2008-01
2009-04
2010-04
7 rows selected.
SCOTT@orcl_11g> SELECT TO_CHAR (start_date, 'MON-YYYY') new_start_date
2 FROM test_sort
3 ORDER BY start_date
4 /
NEW_STAR
--------
MAR-2006
DEC-2006
DEC-2006
APR-2007
JAN-2008
APR-2009
APR-2010
7 rows selected.
SCOTT@orcl_11g>
|
|
|