码迷,mamicode.com
首页 > 其他好文 > 详细

单行函数·上

时间:2016-03-16 21:00:19      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

SQL> -- 大写 小写 首字母大写
SQL> select upper(‘hello world‘) 大写, lower(‘Hello World‘) 小写,initcap(‘hello world‘) 首字母大写 from dual;

大写                   小写                   首字母大写                                                                                                                                               
---------------------- ---------------------- ----------------------                                                                                                                                   
HELLO WORLD            hello world            Hello World                                                                                                                                              

SQL> -- 字节和字符的统计
SQL> select length(‘中国‘) 字符,lengthb(‘中国‘) 字节 from dual;

      字符       字节                                                                                                                                                                                  
---------- ----------                                                                                                                                                                                  
         2          6                                                                                                                                                                                  

SQL> -- 替换字符串里面的o
SQL> select replace(‘hello‘,‘o‘,‘a‘) from dual;

REPLACE(‘H                                                                                                                                                                                             
----------                                                                                                                                                                                             
hella                                                                                                                                                                                                  

SQL> select replace(‘hello‘,‘o‘) from dual;

REPLACE(                                                                                                                                                                                               
--------                                                                                                                                                                                               
hell                                                                                                                                                                                                   

SQL> select lengthb(replace(‘hello‘,‘o‘)) from dual;

LENGTHB(REPLACE(‘HELLO‘,‘O‘))                                                                                                                                                                          
-----------------------------                                                                                                                                                                          
                            4                                                                                                                                                                          

SQL> -- 截取字符串的子串
SQL> select substr(‘hello world‘,3) from dual;

SUBSTR(‘HELLOWORLD                                                                                                                                                                                     
------------------                                                                                                                                                                                     
llo world                                                                                                                                                                                              

SQL> select substr(‘hello world‘,3,4) from dual;

SUBSTR(‘                                                                                                                                                                                               
--------                                                                                                                                                                                               
llo                                                                                                                                                                                                    

SQL> /*
SQL> substr(‘‘,a) 从第a为开始截取到最后
SQL> substr(‘‘,a,b) 从第a位开始截取截取b个长度
SQL> */
SQL> -- 在一个字符串中查找某一个字符或者字符串
SQL> select instr(‘hello world‘,‘o‘) from dual;

INSTR(‘HELLOWORLD‘,‘O‘)                                                                                                                                                                                
-----------------------                                                                                                                                                                                
                      5                                                                                                                                                                                

SQL> select instr(‘hello world‘,‘hello‘) from dual;

INSTR(‘HELLOWORLD‘,‘HELLO‘)                                                                                                                                                                            
---------------------------                                                                                                                                                                            
                          1                                                                                                                                                                            

SQL> -- instr 查询第一次出现某一个字符或者字符串的位置
SQL> -- 左补齐和右补齐
SQL> select lpad(‘hello world‘,15,‘*‘) 左补齐,rpad(‘hello world‘,15,‘@‘) 右补齐 from dual;

左补齐                         右补齐                                                                                                                                                                  
------------------------------ ------------------------------                                                                                                                                          
****hello world                hello world@@@@                                                                                                                                                         

SQL> -- 去掉字符串里面的某一个字符或者子串
SQL> select trim(‘hello world‘,‘hel‘) from dual;
select trim(‘hello world‘,‘hel‘) from dual
                         *
第 1 行出现错误:
ORA-00907: 缺失右括号


SQL> select trim(‘hel‘ from ‘hello world‘) from dual;
select trim(‘hel‘ from ‘hello world‘) from dual
       *
第 1 行出现错误:
ORA-30001: 截取集仅能有一个字符


SQL> select trim(‘h‘ from ‘hello world‘) from dual;

TRIM(‘H‘FROM‘HELLOWO                                                                                                                                                                                   
--------------------                                                                                                                                                                                   
ello world                                                                                                                                                                                             

SQL> -- trim只能去除一个字符,不能去除字符串
SQL> -- 数值函数
SQL> -- round 四舍五入
SQL> select round(42.965,2) 一,round(42.965,1) 二,round(42.965,0) 三,
  2  round(42.965,-1) 四,round(42.965,-2) 五 from dual;

        一         二         三         四         五                                                                                                                                                 
---------- ---------- ---------- ---------- ----------                                                                                                                                                 
     42.97         43         43         40          0                                                                                                                                                 

SQL>  select trunc(42.965,2) 一,trunc(42.965,1) 二,trunc(42.965,0) 三,
  2   trunc(42.965,-1) 四,trunc(42.965,-2) 五 from dual;

        一         二         三         四         五                                                                                                                                                 
---------- ---------- ---------- ---------- ----------                                                                                                                                                 
     42.96       42.9         42         40          0                                                                                                                                                 

SQL> select mod(10,3) from dual;

 MOD(10,3)                                                                                                                                                                                             
----------                                                                                                                                                                                             
         1                                                                                                                                                                                             

SQL> select mod(3,10) from dual;

 MOD(3,10)                                                                                                                                                                                             
----------                                                                                                                                                                                             
         3                                                                                                                                                                                             

SQL> spool off

单行函数·上

标签:

原文地址:http://www.cnblogs.com/877599949yan/p/5284997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!