Solved

Order by Decode( )

Posted on 2004-10-04
2
1,001 Views
Last Modified: 2010-05-18
I have a query created for a report that allows the user to select what information they wish to sort the end report by.  I uderstand that decode automatically type-casts the decode statement based on the first item decoded.  That is the problem: one of the items is a date field the rest are varchar2 fields if the date field is the first in the list then I cannot order by the other fields I get "literal does not match" or "expecting a number format..." if a varchar2 field is first in the list and the date field is selected to order by it is treated as a varchar and is not sorted properly.  Again I understand that is the way decode is supposed to work but there has to be a way around this.  I have tried to type cast from within the decode statement it does not work...at least not the way I tried.

select platform, engineer, end_date, part, complete_date, rcvd_date
from table
where....
order by (:sort, 'ENGINEER', engineer, 'EDD', end_date, 'PART', part)

the above will not correctly sort by end_date

select platform, engineer, end_date, part, complete_date, rcvd_date
from table
where....
order by (:sort,  'EDD', end_date, 'ENGINEER', engineer, 'PART', part)

the above will not correctly sort by engineer or part

any suggestions


0
Comment
Question by:Barovian
2 Comments
 
LVL 20

Expert Comment

by:dsacker
Comment Utility
Try:

order by decode(:sort,'ENGINEER',engineer,'EDD',to_char(end_date,'yyyymmddhh24miss'),'PART',part)

If you don't need the hours, take off the hh24miss.
0
 
LVL 6

Accepted Solution

by:
izblank earned 500 total points
Comment Utility
Try this:

order by decode(:sort,  'EDD', end_date, NULL),
   decode(:sort, 'ENGINEER', engineer, NULL),
   decode(:sort, 'PART', part,NULL)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious sideā€¦
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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.
Via a live example, show how to take different types of Oracle backups using RMAN.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now