Solved

Order by Decode( )

Posted on 2004-10-04
2
1,022 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle collections 2 41
Moving Oracle Database to other server 2 46
SQL Syntax Question 9 55
DB Shutdown Automatically 11 32
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

737 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