(Turn) Oracle common function 1: digital function

1, abs(n) returns the absolute value of n

[plain]  view plain copy  
 
  1. SQL> select abs(-1),abs(1) from dual;  
  2.   
  3.    ABS(-1)     ABS(1)  
  4. ---------- ----------  
  5.          1          1  

2, ceil(n) returns the smallest integer greater than or equal to n

[plain]  view plain copy  
 
  1. SQL> select ceil(10),ceil(10.5),ceil(-10.5) from dual;  
  2.   
  3.   CEIL (10) CEIL (10.5) CEIL (-10.5)  
  4. ---------- ---------- -----------  
  5.         10         11         -10  

3, floor(n) returns the largest integer less than or equal to n

[plain]  view plain copy  
 
  1. SQL> select floor(10),floor(10.5),floor(-10.5) from dual;  
  2.   
  3.  FLOOR(10) FLOOR(10.5) FLOOR(-10.5)  
  4. ---------- ----------- ------------  
  5.         10          10          -11  

4, greatest(expr[,expr...]) returns the maximum value of the parameter list, which can be used for numbers, strings, dates

[plain]  view plain copy  
 
  1. SQL> select greatest(1,2,3) from dual;  
  2.   
  3. GREATEST(1,2,3)  
  4. ---------------  
  5.               3  
  6.   
  7. SQL> select greatest('a','b') from dual;  
  8.   
  9. GR  
  10. --  
  11. b  
  12.   
  13. SQL> select greatest(to_date('2011','yyyy'),to_date('2012','yyyy')) from dual;  
  14.   
  15. GREATEST(TO_DA  
  16. --------------  
  17. 01-11月-12  

如果参数列表为混合类型,Oracle在选择最大值之前将选择第一个参数类型作为基准类型并试图将其他参数转换为第一个参数的类型

[plain]  view plain  copy
 
  1. SQL> select greatest(1,'abc') from dual;  
  2. select greatest(1,'abc') from dual  
  3.                   *  
  4. ERROR at line 1:  
  5. ORA-01722: invalid number  
  6.   
  7.   
  8. SQL> select greatest(1,'111') from dual;  
  9.   
  10. GREATEST(1,'111')  
  11. -----------------  
  12.               111  

5,least(expr[,expr...]) 选择参数列表最小值,和greatest函数相反,参数规则相同

6,mod(m,n) m除以n的余数

[plain]  view plain  copy
 
  1. SQL> select mod(11,10) from dual;  
  2.   
  3. MOD(11,10)  
  4. ----------  
  5.          1  

7,power(m,n) m的n次方,m为非0数字,如果m为正数,n可以为正数或负数,如果m为负数,n必须为正整数

[plain]  view plain  copy
 
  1. SQL> select power(2,2),power(-1,2),power(1,0) from dual;  
  2.   
  3. POWER(2,2) POWER(-1,2) POWER(1,0)  
  4. ---------- ----------- ----------  
  5.          4           1          1  
  6.   
  7. SQL> select power(-2,0.5) from dual;  
  8. select power(-2,0.5) from dual  
  9.              *  
  10. ERROR at line 1:  
  11. ORA-01428: argument '-2' is out of range  

8,round(m,n) 对十进制数字m,根据n进行四舍五入计算

[plain]  view plain  copy
 
  1. SQL> select round(123.5),round(123.46,1),round(126.45,-1) from dual;  
  2.   
  3. ROUND(123.5) ROUND(123.46,1) ROUND(126.45,-1)  
  4. ------------ --------------- ----------------  
  5.          124           123.5              130  

9,sign(n) 返回n的符号,n为负数返回-1,n为0返回0,n为正数返回1

[plain]  view plain  copy
 
  1. SQL> select sign(-11),sign(0),sign(99) from dual;  
  2.   
  3.  SIGN(-11)    SIGN(0)   SIGN(99)  
  4. ---------- ---------- ----------  
  5.         -1          0          1  

10,sqrt(n) n的平方根,n不能为负数

[plain]  view plain  copy
 
  1. SQL> select sqrt(4),sqrt(0),sqrt(80) from dual;  
  2.   
  3.    SQRT(4)    SQRT(0)   SQRT(80)  
  4. ---------- ---------- ----------  
  5.          2          0 8.94427191  
  6.   
  7. SQL> select sqrt(-3) from dual;  
  8. select sqrt(-3) from dual  
  9.             *  
  10. ERROR at line 1:  
  11. ORA-01428: argument '-3' is out of range  

11, trunc(m[,n]) intercepts the number m according to the specified number of digits n (only interception, no rounding)

[plain]  view plain copy  
 
  1. SQL> select trunc(999.99),trunc(999.99,1),trunc(999.99,-2) from dual;  
  2.   
  3. TRUNC(999.99) TRUNC(999.99,1) TRUNC(999.99,-2)  
  4. ------------- --------------- ----------------  
  5.           999           999.9              900  

 

 

 

REFS:http://blog.csdn.net/a19881029/article/details/8161323

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326398993&siteId=291194637