Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sql Query for Decode function with like condition

User_UMJZ5Mar 12 2017 — edited Mar 14 2017

Hi All,

Im new to decode function and trying to learn how to use decode function instead of case stateement.I have taken common table that is present in oracle database 'EMP'.for the below enames im trying to display specific location that is mentioned in decode condition but im not getting the proper result and throwing error ..I have consider below enames based for use on like condition mentioned below for the below empnames i want to display specifci location names mentioned in my decode script and for remaining enames it should be display location  as null

Input values:

select EMPNO,ENAME FROM emp

where ename like 'S%' OR

ename like 'A%' OR

ename like 'J%' OR

ename like 'M%' OR

ename like 'B%'

order by ename asc

 

empnoename
7876ADAMS
7499ALLEN
7698BLAKE
7900JAMES
7566JONES
7654MARTIN
7934MILLER
7788SCOTT
7369SMITH

Script used to get below output but im getting error.how to achieve below output by using decode function instead of case and if conditions.

select empno,ename,decode(ename,'S%','SA','A%,'ASIA','J%','JAPAN','M%','MALASIA','B%','BANGALORE') as LOCATIONS from emp

output:

   

empnoenameLocations
7876ADAMSASIA
7499ALLENASIA
7698BLAKEBANGALORE
7900JAMESJAPAN
7566JONESJAPAN
7654MARTINMALASIA
7934MILLERMALASIA
7788SCOTTSA
7369SMITHSA
This post has been answered by Frank Kulash on Mar 12 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 11 2017
Added on Mar 12 2017
10 comments
5,280 views