欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Oracle的字符串处理函数

程序员文章站 2022-06-02 12:51:17
...

Oracle的字符串处理函数

1、将字符串转换为大写字符

格式:

UPPER(string)

举例:

SQL> select UPPER('I am a student!') AS RESULT from dual;

RESULT
---------------
I AM A STUDENT!

2、将字符串转换为小写字符

格式:

LOWER(string)

举例:

SQL> select LOWER('I am a student!') AS RESULT from dual;

RESULT
---------------
i am a student!

3、将字符串中每个单词的首字母转换为大写字符

格式:

INITCAP(string)

举例:

SQL> select INITCAP('I am a student!') AS RESULT from dual;

RESULT
---------------
I Am A Student!

4、字符串截取函数

格式:

SUBSTR(string,start [,count])

说明:
(1)如果省略count参数,则一直截取到字符串的结尾;
(2)start参数如果为负值,则从右边开始计算截取位置。

举例:

SQL> col RESULT format a20
SQL> select SUBSTR('I am a student!',3,2) as RESULT from dual;

RESULT
--------------------
am

SQL> select SUBSTR('I am a student!',3) as RESULT from dual;

RESULT
--------------------
am a student!

SQL> select SUBSTR('I am a student!',-8,7) as RESULT from dual;

RESULT
--------------------
student

SQL> select SUBSTR('I am a student!',-8) as RESULT from dual;

RESULT
--------------------
student!

5、计算字符串长度

格式:

LENGTH(string)

举例:

SQL> select ename,LENGTH(ename) as name_length from emp where rownum <= 5;

ENAME	   NAME_LENGTH
---------- -----------
SMITH		     5
ALLEN		     5
WARD		     4
JONES		     5
MARTIN		     6

6、字符串连接

格式:

(1) string1 || string2
(2) CONCAT(string1,string2)

举例:

SQL> select concat('Henan ','Xinxiang') as RESULT from dual;

RESULT
--------------------
Henan Xinxiang

SQL> select 'Beijing,'||'China' as RESULT from dual;

RESULT
--------------------
Beijing,China

7、查找子串位置

格式:

INSTR(string,substring[,start [,occurrence ] ] ) 

说明:
(1)该函数用于查找substring在string中的位置,如果找不到子串,则返回0;
(2)如果有多个查找结果,默认从最左边开始返回第一个子串的位置。如果指定了start,则从start开始查找。如果指定了occurrence,则返回第occurrence个子串的位置;
(3)start可以是负值,表示从右边开始计算查找的开始位置,并且从右向左查找子串。

举例:

SQL> select INSTR('The People *!','people') as RESULT from dual;

    RESULT
----------
	 0

SQL> select INSTR('The People *!','People') as RESULT from dual;

    RESULT
----------
	 5
	 
SQL> select INSTR('The People *!','e') as RESULT from dual;

    RESULT
----------
	 3

SQL> select INSTR('The People *!','e',4) as RESULT from dual;

    RESULT
----------
	 6

SQL> select INSTR('The People *!','e',1,2) as RESULT from dual;

    RESULT
----------
	 6

SQL> select INSTR('The People *!','o',-3) as RESULT from dual;

    RESULT
----------
	21

8、删除字符串的首尾字符

格式:

LTRIM(string)
RTRIM(string)
TRIM(string)

TRIM(char from string)

说明:
(1)默认删除首尾空格;
(2)char只能是一个字符,不能是字符串;
(3)利用【char from string】可以删除任意的首尾字符。

举例:

SQL> select 'China,' || LTRIM(' Henan ') || ',Xinxiang.' as RESULT from dual;

RESULT
----------------------
China,Henan ,Xinxiang.

SQL> select 'China,' || RTRIM(' Henan ') || ',Xinxiang.' as RESULT from dual;

RESULT
----------------------
China, Henan,Xinxiang.

SQL> select 'China,' || TRIM(' Henan ') || ',Xinxiang.' as RESULT from dual;

RESULT
---------------------
China,Henan,Xinxiang.

SQL> select TRIM('*' from '**Henan*Xinxiang*') as RESULT1 from dual;

RESULT1
--------------------
Henan*Xinxiang

9、填充字符

格式:

LPAD(string,num,string2)
RPAD(string,num,string2)

说明:利用string2在string的左边或右边进行填充,使string的总长度变成num。

举例:

SQL> select LPAD('stu',10,'12') as RESULT from dual;

RESULT
----------
1212121stu

SQL> select RPAD('stu',10,'12') as RESULT from dual;

RESULT
----------
stu1212121

10、替换字符串

格式:

REPLACE(string,string1,string2)

说明:把string中的所有string1替换成string2。

举例:

SQL> select REPLACE('I am a student!','student','teacher') as RESULT from dual;

RESULT
---------------
I am a teacher!

SQL> select REPLACE('aabbbccbb','b','1') as RESULT from dual;

RESULT
---------
aa111cc11