Oracle函数有很多种,把平时用到的罗列下。
(1)concat(str1,str2)字符串拼接函数
select concat('Hello ','World') from dual;--等价于select 'Hello '||'World' from dual;
(2)initcap(str)将每个单词首字母大写,其他字母小写
select initcap('hello world!') from dual; --返回结果为'Hello World!'select initcap('HELLO WORLD!') from dual; --返回结果为'Hello World!'
(3)lower(str)将字符串转换为小写
select lower('Hello World!') from dual;--返回 hello world!
(4)upper(str)将字符串转换为大写
select upper('Hello World!') from dual;--返回HELLO WORLD!
(5)instr(x,find_string[,start][,occurrence])返回指定字符串在某字符串中的位置,可以指定搜索的开始位置和返回第几次搜索出来的结果
----------搜索时下标从1开始计算select instr('Hello World!','o') from dual;--从1位置开始搜索,返回第一次出现的o的位置,结果为5select instr('Hello World!','o',6) from dual;--从6位置开始搜索,返回第一次出现的o的位置,结果为8select instr('Hello World!','o',1,2) from dual;--从1位置开始搜索,返回第二次出现o的位置,结果为8
(6)substr(x,start[,length])返回字符串中的指定的字符,这些字符从字符串的第start个位置开始,长度为length个字符;如果start是负数,则从x字符串的末尾开始算起;如果 length省略,则将返回一直到字符串末尾的所有字符
select substr('Hello World',3) from dual; --返回结果为'llo World'select substr('Hello World',-3) from dual;--返回结果为'rld'select substr('Hello World',3,2) from dual;--返回结果为'll'select substr('Hello World',-7,4) from dual;--返回结果为'o Wo'
(7)length(str)返回表达式中的字符数
select length('Hello World!') from dual;--返回结果为12select length('张三') from dual;--返回结果为2
(8)lengthb(str)返回表达式中的字节数
select lengthb('Hello World!') from dual;--返回结果为12select lengthb('张三') from dual;--返回结果为6
(9)lpad(str,width[,pad_string])当字符串长度不够时,左填充补齐,可以指定补齐时用什么字符补齐,若不指定,则以空格补齐
select lpad('Hello World!',20) from dual;--返回结果为' Hello World!'select lpad('Hello World!',20,'*') from dual;--返回结果为'********Hello World!'
(10)rpad(str,width[,pad_string])当字符串长度不够时,右填充补齐,原理同左填充
select rpad('Hello World!',20) from dual;--返回结果为'Hello World! 'select rpad('Hello World!',20,'*+') from dual;--返回结果为'Hello World!*+*+*+*+'
(11)ltrim(x[,trim_string])从字符串左侧去除指定的所有字符串,若没有指定去除的字符串,则默认去除左侧空白符(包含空格,tab等)
--后面不加 [,trim_string] 用来去除空白使用返回结果为'Hello World! 'select ltrim(' Hello World! ') from dual;--[,trim_string] 里面的元素可以是多个,是按照每个字符的内容去除的。例1:*和+ 是去除的元素,左侧只要连续包含*或+就可以被去除,如果断开加入其它内容那么不会继续去除 例2 -例1-返回结果为'Hello World!***+*'例1:select ltrim('***+*Hello World!***+*','*+') from dual;--例2返回结果'n*+*Hello World!***+*' 例2:select ltrim('**n*+*Hello World!***+*','*+') from dual;--如果想和例1一样的结果,[,trim_string] 中加入一个'n' 位置不重要例2:select ltrim('**n*+*Hello World!***+*','*+n') from dual;
(12)rtrim(x[,trim_string])从字符串右侧去除指定的所有字符串,原理同ltrim()
select rtrim(' Hello World! ') from dual;--返回结果为' Hello World!'select rtrim('***+*Hello World!***+*','*+') from dual;--返回结果为'***+*Hello World!'
(13)trim(trim_string from x)从字符串两侧去除指定的所有字符串 注意,ltrim()和rtrim()的截取集可以使多个字符,但trim的截取集只能有一个字符
--默认左右去空白select trim(' ***+*Hello World!***+* ') from dual;--只能有一个字符 --结果'***+*Hello World!***+*'select trim('+' from '+***+*Hello World!***+*+') from dual;
(14)nvl(x,value)将一个NULL转换为另外一个值,如果x为NULL,则返回value,否则返回x值本身
select nvl(address,'北京市') from student;
(15)nvl2(x,value1,value2),如果x不为NULL,返回value1,否则,返回value2
select nvl2(address,'有地址','无地址') from student;
(16)replace(x,search_string,replace_string),从字符串x中搜索search_string字符串,并使用replace_string字符串替换。
select replace('Hello World!','o','HA') from dual;
(1)abs(value)返回value的绝对值
select abs(-10) from dual;--返回结果为10
(2)ceil(value)返回大于等于value的最小整数
select ceil(2.3) from dual; --返回结果为3
(3)floor(value)返回小于等于value的最大整数
select floor(2.3) from dual; --返回结果为2
(4)trunc(value,n)对value进行截断,如果n>0,保留n位小数;n<0,则保留-n位整数位;n=0,则去掉小数部分
select trunc(555.666) from dual; --返回结果为555,不加n时默认去掉小数部分select trunc(555.666,2) from dual;--返回结果为555.66select trunc(555.666,-2) from dual;--返回结果为500
(5)round(value,n)对value进行四舍五入,保存小数点右侧的n位。如果n省略的话,相当于n=0的情况
select round(555.666) from dual;--返回结果为556,不加n时默认去掉小数部分select round(555.666,2) from dual;--返回结果为555.67select round(555.666,-2) from dual;--返回结果为600
注意:
1.trunc和round用法类似,只不过trunc是硬生生截取,并不进行四舍五入,而round进行截取时四舍五入
2.都还可以对日期的截取,可以参考写的日期函数笔记
select round(sysdate,'year') from dual;select trunc(sysdate,'year') from dual;
3.转换函数
将值从一种类型转换成另外一种类型,或者从一种格式转换为另外一种格式
(1)to_char(x[,format]):将x转化为字符串。 format为转换的格式,可以为数字格式或日期格式
select to_char('12345.67') from dual; --返回结果为12345.67select to_char('12345.67','99,999.99') from dual; --返回结果为12,345.67
(2)to_number(x [, format]):将x转换为数字。可以指定format格式
select to_number('970.13') + 25.5 from dual;select to_number('-$12,345.67', '$99,999.99') from dual;
(3)cast(x as type):将x转换为指定的兼容的数据库类型
select cast(12345.67 as varchar2(10)),cast('05-7月-07' as date), cast(12345.678 as number(10,2)) from dual;
(4)to_date(x [,format]):将x字符串转换为日期
--日期函数较多,详见日期函数专题select to_date('2012-3-15','YYYY-MM-DD') from dual
1.常用函数
(1)avg(x):返回x的平均值
select avg(grade) from tablename; --x某列的值
(2)count(x):返回统计的行数
select count(name) from tablename;--null不统计select count(1) from tablename;--统计全部的行数--之前有说法比count(1)慢,其实是一样的count(*) ,count(100)效率也一样select count(*) from tablename;
(3)max(x):返回x的最大值
select max(grade) from tablename;
(4)min(x):返回x的最小值
select min(grade) from tablename;
(5)sum(x):返回x的总计值
select sum(grade) from tablename;
(6) over() partition 详见第一篇文章《窗口函数sql计算移动平均数和累计值》
上一条:南非世界杯((体育)全力备战 亚运亮剑——专访中国女垒主教练唐昌冬)