学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序员来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函数及示例,一是为了和大家分享,二是可以在以后工作中忘记了随时查阅。
1.lower——将字符串转化为小写
select lower('Hello World!') from dual;
2.upper——将字符串转化为大写
select upper('Hello World!') from dual;
3.length——返回字符串的长度
select length('Hello World!') from dual;--返回结果为12
4.concat——字符串拼接
select concat('Hello ','World') from dual;
5.initcap——将每个单词首字母大写,其他字母小写
select initcap('hello world!') from dual; --返回结果为'Hello World!'
select initcap('HELLO WORLD!') from dual; --返回结果为'Hello World!'
6.instr——INSTR(C1,C2,I,J)
在一个字符串中搜索指定的字符,返回发现指定的字符的位置;
C1 被搜索的字符串
C2 希望搜索的字符串
I 搜索的开始位置,默认为1
J 出现的位置,默认为1
select instr('Hello World!','o') from dual;--从1位置开始搜索,返回第一次出现的o的位置,结果为5
select instr('Hello World!','o',6) from dual;--从6位置开始搜索,返回第一次出现的o的位置,结果为8
select instr('Hello World!','o',1,2) from dual;--从1位置开始搜索,返回第二次出现o的位置,结果为8
7.lpad——当字符串长度不够时,左填充补齐,可以指定补齐时用什么字符;若不指定,则以空格补齐
select lpad('Hello World!',20) from dual;--返回结果为' Hello World!'
8.rpad——当字符串长度不够时,右填充补齐,可以指定补齐时用什么字符补齐;若不指定,则以空格补齐
select lpad('Hello World!',20) from dual;--返回结果为'Hello World! '
9.trim——TRIM('s' from 'string')
LEADING 剪掉前面的字符
TRAILING 剪掉后面的字符
如果不指定,默认为空格符
select trim(' Hello World! ') from dual;--返回结果为'Hello World!'
10.ltrim——LTRIM(X,[TRIM_STRING])
LTRIM 删除左边出现的字符串, 默认为空字符串
select ltrim(' Hello World! ') from dual;--返回结果为'Hello World! '
11.rtrim——RTRIM(X, [TRIM_STRING])
RTRIM 删除右边出现的字符串TRIM_STRING,默认为空字符串
select rtrim(' Hello World! ') from dual;--返回结果为' Hello World!'
12.nvl——NVL(X, VALUE)
如果X是空值,返回VALUE,否则返回X
select nvl(NULL, '2') from dual;
select nvl('33', '2') from dual;
13.nvl2——NVL2(X, VALUE1, VALUE2)
如果X是空值,返回VALUE1, 否则返回VALUE2
select nvl2(NULL, '3', '4') from dual;
14.replace——REPLACE('string','s1','s2')
string 希望被替换的字符或变量
s1 被替换的字符串
s2 要替换的字符串
select replace('Hello World!','o','HA') from dual;
15.substr——SUBSTR(string,start,count):取子字符串,从start开始,取count个
select substr('you are right!, come on', 3, 30) from dual;