what is DECODE function used for?

来源:互联网 发布:郑州淘宝学校 编辑:程序博客网 时间:2024/05/18 14:13

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

 

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

 

Let us see this with an example:

 

 Consider a table named A which has following namely:Empname,sex,salary

 

select * from A

 

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

 

 

 

 

 

原创粉丝点击