Oracle常用字符串函数

(1)lengthb/length

计算字符串长度

lengthb求得是字节(Byte,1Byte=8bit)长度length求得是字符长度

1 2 select lengthb('中') from dual; select length('中') from dual;

ZHS16GBK下,lengthb(‘中’)为2字节,length(‘中’)为1(个字符),即一个字符占两个字节

数据库中存储的CHAR(19) 表示占19个字节。

(2)SUBSTR

SUBSTR用于截取字符串的子串,需要注意的是Oracle 数据库中字符串的下标是从 1 开始而不是从 0 开始的。该函数的语法如下:

1 SUBSTR( string, start [, length] )

string是要截取的字符串start是要开始截取的位置length是要截取的子串长度(可选)

e.g.

1 select substr('abcdefg',0,3) from dual;

输出

abc

1 select substr('abcdefg',1,3) from dual;

输出

abc

1 select substr('abcdefg',2,3) from dual;

输出

bcd

1 select substr('abcdefg',-3,3) from dual;

输出

efg

(3)INSTR

INSTR 在字符串中搜索指定字符,返回发现指定字符的位置。该函数的语法如下:

1 INSTR( string, substring [, start_position [, occurrence ]] )

string是要搜索的字符串substring是要查找的子start_position是要开始搜索的位置(可选)occurrence是要查找的子串出现的次数(可选)

e.g.

1 Select instr('oracle training','ai') From dual;

输出

10

(4)CONCAT

CONCAT连接两个字符串

e.g.

1 SELECT CONCAT('Hello ', 'World', '!') FROM dual;

输出

Hello World!

按要求更新指定列:

1 Update t_skzy Set website=concat('http://',website) Where website Not Like 'http%' And website Like '%.%'

(5)REPLACE

REPLACE用于替换字符串的指定子串。该函数的语法如下:

1 REPLACE( string, substring1 [, substring2] )

string是要替换子串的字符串substring1是要被替换的子串substring2是用来替换substring1的字符串(可选)

e.g.

1 SELECT REPLACE('Hello World!', 'Hello', 'Goodbye') FROM dual;

输出:

“Goodbye World!”

(6)TRIM, LTRIM, RTRIM

TRIM:去除字符串的空格或指定字符

语法如下:

1 TRIM([leading|trailing|both] [trim_character] FROM string)

leading|trailing|both:可选参数,用于指定去除字符串的空格或指定字符是在字符串的前面、后面还是两边,默认为 both。trim_character:可选参数,用于指定要去除的字符,默认为字符串中的空格。string:必需参数,要去除空格或指定字符的字符串。

LTRIM :去除字符串左侧的空格或指定字符。

语法如下:

1 LTRIM([trim_character] FROM string)

trim_character:可选参数,用于指定要去除的字符,默认为字符串中的空格。string:必需参数,要去除空格或指定字符的字符串。

e.g.

1 Select ltrim('trimtest ltrim ','trim') From dual

输出

test ltrim

RTRIM :去除字符串右侧的空格或指定字符。

语法如下:

1 RTRIM([trim_character] FROM string)

trim_character:可选参数,用于指定要去除的字符,默认为字符串中的空格。string:必需参数,要去除空格或指定字符的字符串。

(7)ASCII

ASCII返回给定字符串中第一个字符的ASCII代码值。

e.g.

1 SELECT ASCII('A') FROM dual;

输出

65

(8)NVL

1 NVL( string1, replace_with)

如果string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值。

例如,以下查询将返回一个包含员工的职务和部门名称的结果,如果员工所在的部门为空,则返回“Unknown Department”:

1 2 SELECT job_id, NVL(department_name, 'Unknown Department') FROM employees e LEFT JOIN departments d ON e.department_id = d.department_id;

(9)INITCAP,LOWER,UPPER

INITCAP 将字符串第一个字母变为大写LOWER 将字符串所有字母小写UPPER 将字符串所有字母大写

来源:微点阅读   https://www.weidianyuedu.com

参考链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。