Solved

Order by Decode( )

Posted on 2004-10-04
2
1,015 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
ID: 12219058
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
ID: 12219151
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.

Question has a verified solution.

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

Suggested Solutions

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to take different types of Oracle backups using RMAN.

829 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