码迷,mamicode.com
首页 > 数据库 > 详细

SQL_字符操作函数

时间:2014-10-18 18:23:45      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:sql   字符操作函数   

原创作品,出自 “深蓝的blog” 博客,欢迎转载,转载时请务必注明以下出处,否则追究版权法律责任。

深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/40213181 

思维导图:

bubuko.com,布布扣

用简单的例子演示,如下列举,用最简单的操作体会字符操作函数的用法:

concat函数

SQL> select ename,job,concat(ename,job) from emp;
ENAME      JOB       CONCAT(ENAME,JOB)
---------- --------- -------------------
SMITH      CLERK     SMITHCLERK
ALLEN      DBA       ALLENDBA
WARD       DBA       WARDDBA
JONES      MANAGER   JONESMANAGER
MARTIN     DBA       MARTINDBA
BLAKE      DBA       BLAKEDBA
CLARK      MANAGER   CLARKMANAGER
SCOTT      ANALYST   SCOTTANALYST
KING       PRESIDENT KINGPRESIDENT
TURNER     DBA       TURNERDBA
ADAMS      CLERK     ADAMSCLERK
JAMES      DBA       JAMESDBA
FORD       ANALYST   FORDANALYST
MILLER     CLERK     MILLERCLERK
14 rows selected
SQL> select ename,job,concat(ename,'s job is ' || job) from emp;
ENAME      JOB       CONCAT(ENAME,'SJOBIS'||JOB)
---------- --------- ----------------------------
SMITH      CLERK     SMITHs job is CLERK
ALLEN      DBA       ALLENs job is DBA
WARD       DBA       WARDs job is DBA
JONES      MANAGER   JONESs job is MANAGER
MARTIN     DBA       MARTINs job is DBA
BLAKE      DBA       BLAKEs job is DBA
CLARK      MANAGER   CLARKs job is MANAGER
SCOTT      ANALYST   SCOTTs job is ANALYST
KING       PRESIDENT KINGs job is PRESIDENT
TURNER     DBA       TURNERs job is DBA
ADAMS      CLERK     ADAMSs job is CLERK
JAMES      DBA       JAMESs job is DBA
FORD       ANALYST   FORDs job is ANALYST
MILLER     CLERK     MILLERs job is CLERK
14 rows selected

 

 

substr函数 

SQL> select ename,concat(ename,job),length(ename),instr(ename,'a') from emp where substr(job,1,5)='ANALY';
ENAME      CONCAT(ENAME,JOB)   LENGTH(ENAME) INSTR(ENAME,'A')
---------- ------------------- ------------- ----------------
SCOTT      SCOTTANALYST                    5                0
FORD       FORDANALYST                     4                0 
  

length函数 

SQL> select ename,length(ename) from emp;
ENAME      LENGTH(ENAME)
---------- -------------
SMITH                  5
ALLEN                  5
WARD                   4
JONES                  5
MARTIN                 6
BLAKE                  5
CLARK                  5
SCOTT                  5
KING                   4
TURNER                 6
ADAMS                  5
JAMES                  5
FORD                   4
MILLER                 6
14 rows selected 
 

instr函数 

SQL> select ename,instr(ename,'A') from emp ;
ENAME      INSTR(ENAME,'A')
---------- ----------------
SMITH                     0
ALLEN                     1
WARD                      2
JONES                     0
MARTIN                    2
BLAKE                     3
CLARK                     3
SCOTT                     0
KING                      0
TURNER                    0
ADAMS                     1
JAMES                     2
FORD                      0
MILLER                    0
14 rows selected

 
SQL> select ename,concat(ename,job),length(ename),instr(ename,'a') from emp;
ENAME      CONCAT(ENAME,JOB)   LENGTH(ENAME) INSTR(ENAME,'A')
---------- ------------------- ------------- ----------------
SMITH      SMITHCLERK                      5                0
ALLEN      ALLENDBA                        5                0
WARD       WARDDBA                         4                0
JONES      JONESMANAGER                    5                0
MARTIN     MARTINDBA                       6                0
BLAKE      BLAKEDBA                        5                0
CLARK      CLARKMANAGER                    5                0
SCOTT      SCOTTANALYST                    5                0
KING       KINGPRESIDENT                   4                0
TURNER     TURNERDBA                       6                0
ADAMS      ADAMSCLERK                      5                0
JAMES      JAMESDBA                        5                0
FORD       FORDANALYST                     4                0
MILLER     MILLERCLERK                     6                0
14 rows selected 
 

lpad函数

SQL> select ename,lpad(ename,10,'-') from emp;
ENAME      LPAD(ENAME,10,'-')
---------- --------------------
SMITH      -----SMITH
ALLEN      -----ALLEN
WARD       ------WARD
JONES      -----JONES
MARTIN     ----MARTIN
BLAKE      -----BLAKE
CLARK      -----CLARK
SCOTT      -----SCOTT
KING       ------KING
TURNER     ----TURNER
ADAMS      -----ADAMS
JAMES      -----JAMES
FORD       ------FORD
MILLER     ----MILLER
14 rows selected 
 

trim函数

SQL> select trim('s' from 'sdfde') from dual;
TRIM('S'FROM'SDFDE')
--------------------
dfde

SQL> select trim('s' from 'sdsfsde') from dual;
TRIM('S'FROM'SDSFSDE')
----------------------
dsfsde

SQL> select trim('s' from 'ssdsfsde') from dual;
TRIM('S'FROM'SSDSFSDE')
-----------------------
dsfsde

 

原创作品,出自 “深蓝的blog” 博客,欢迎转载,转载时请务必注明以下出处,否则追究版权法律责任。

深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/40213181 

 

SQL_字符操作函数

标签:sql   字符操作函数   

原文地址:http://blog.csdn.net/huangyanlong/article/details/40213181

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