GeekInterview.com
  I am new, Sign me up!
 
Home Oracle
 

What is DECODE function used for?

 


DECODE is used to decode a CHAR or VARCHAR2 or NUMBER into any of several different character strings or numbers based on value. That is DECODE does a value-by-value substitution. For every value that is given in the DECODE function it makes an if then check and matches the value. The general format of DECODE function is given below:


DECODE(value,if1,then1,if2,then2,……,else)


In the above the else can be a function or a literal or another column.


Let us see this with an example


Consider a table named as exforsys which has the following attributes namely:
Empname, sex, salary


Select * from exforsys;


Gives output as


Empname     sex   salary
------------ ---- -------
Priya         F     20000
Sri           F     10000
Sam           M     20000
Raj           M     20000
Sita          F     10000


In the above if we give the DECODE function as below namely:


Select empname, sex, DECODE(salary,’20000’,’50000’,’70000’) from exforsys;


The output of above DECODE function would result as below:


Empname     sex     salary
------------ ----   -------
Priya          F     50000
Sri            F     70000
Sam            M     50000
Raj            M     50000
Sita           F     70000



Read Next: What is a view



 

 

Comments


mohammad zubair said:

  It is used instead of if then else statement.
regards,
mohammad
April 14, 2008, 6:35 am

TMoore said:

  I'm new to Oracle SQL and need an example of how to use DECODE to determine is a date falls within a specified range. For example: If 7/12/08 is between 6/1/08 and 8/31/08 say 'Yes' else say 'No.' I know this should be simple but I get this error 'ORA-00936: missing expression' when running the code below:

DECODE(e.EFFECTIVE_END_DATE,BETWEEN '&sdate' AND '&edate','Yes','No') status_change

Thanks in advance
July 18, 2008, 9:25 am

Vijay said:

  1) how to use nested decode
2) can we use conditional operators inside decode i.e if we want to compare 2 queries?
September 11, 2008, 2:56 am

bhupesh_kec said:

  It is a better and cleaner form of using If then Else.

Regards,
Bhupesh
September 12, 2008, 12:25 am

Maheshwari said:

  can we use decode within decode function
September 16, 2008, 6:18 am

KW Wong said:

  For us to know how to program a high security code.
September 17, 2008, 10:01 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape