oracle中decode函数

1.decode(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)

使用command 为0时就是none,为2时就是insert

 select sid,serial#,username,

 

  DECODE(command,

 

  0,’None’,

 

  2,’Insert’,

 

  3,’Select’,

 

  6,’Update’,

 

  7,’Delete’,

 

  8,’Drop’,

 

  ‘Other’) cmmand

 

  from v$session where username is not null;

2.比较大小

select decode(sign(变量1-变量2),-1,变量1,变量2) from dual; --取较小值

  sign()函数根据某个值是0、正数还是负数,分别返回0、1、-1

3 DECODE实现表的转置

select sign( 100 ),sign(- 100 ),sign( 0 ) from dual;

  SIGN(100) SIGN(-100) SIGN(0)
  ———- ———- ———-
  1 -1 0

二、a=10,b=20
  则sign(a-b)返回-1
  关于函数取值 1. 取四舍五入的几位小数
select round(1.2345, 3) from dual;
结果:1.235
2. 保留两位小数,只舍
select trunc(1.2345, 2) from dual;
结果:1.23

select trunc(1.2399, 2) from dual;
结果:1.23
3.取整数
返回大于或等于x的最大整数: 
        SQL>         select         ceil(23.33)         from         dual; 
结果:   24 

返回等于或小于x的最大整数: 
        SQL>         select         floor(23.33)         from         dual; 
结果:   23 
  
返回舍入到小数点右边y位的x值:rcund(x,[y]) 
        SQL>         select         round(23.33)         from         dual; 
结果:    23 
  
返回截尾到y位小数的x值:trunc(x,[y]) 
        SQL>         select         trunc(23.33)         from         dual; 
结果:   23 

 

猜你喜欢

转载自shoushounihao.iteye.com/blog/1888260