oracle中的一些常用函数及示例
前言
学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函数及示例,一是为了和大家分享,二是可以在以后工作中忘记了随时查阅。废话不多说,下面直接上函数。
一.单行函数
只处理单个行,并且为每行返回一个结果。
1.字符函数
(1)concat(str1,str2)字符串拼接函数
selectconcat('Hello','World')fromdual; --等价于 select'Hello'||'World'fromdual;
(2)initcap(str)将每个单词首字母大写,其他字母小写
selectinitcap('helloworld!')fromdual;--返回结果为'HelloWorld!' selectinitcap('HELLOWORLD!')fromdual;--返回结果为'HelloWorld!'
(3)instr(x,find_string[,start][,occurrence])返回指定字符串在某字符串中的位置,可以指定搜索的开始位置和返回第几次搜索出来的结果
----------搜索时下标从1开始计算 selectinstr('HelloWorld!','o')fromdual;--从1位置开始搜索,返回第一次出现的o的位置,结果为5 selectinstr('HelloWorld!','o',6)fromdual;--从6位置开始搜索,返回第一次出现的o的位置,结果为8 selectinstr('HelloWorld!','o',1,2)fromdual;--从1位置开始搜索,返回第二次出现o的位置,结果为8
(4)length(str)返回表达式中的字符数
selectlength('HelloWorld!')fromdual;--返回结果为12 selectlength('张三')fromdual;--返回结果为2
(5)lower(str)将字符串转换为小写
selectlower('HelloWorld!')fromdual;
(6)lengthb(str)返回表达式中的字节数
selectlengthb('HelloWorld!')fromdual;--返回结果为12 selectlengthb('张三')fromdual;--返回结果为6
(7)upper(str)将字符串转换为大写
selectupper('HelloWorld!')fromdual;
(8)lpad(str,width[,pad_string])当字符串长度不够时,左填充补齐,可以指定补齐时用什么字符补齐,若不指定,则以空格补齐
selectlpad('HelloWorld!',20)fromdual;--返回结果为'HelloWorld!' selectlpad('HelloWorld!',20,'*')fromdual;--返回结果为'********HelloWorld!'
(9)rpad(str,width[,pad_string])当字符串长度不够时,右填充补齐,原理同左填充
selectrpad('HelloWorld!',20)fromdual;--返回结果为'HelloWorld!' selectrpad('HelloWorld!',20,'*+')fromdual;--返回结果为'HelloWorld!*+*+*+*+'
(10)ltrim(x[,trim_string])从字符串左侧去除指定的所有字符串,若没有指定去除的字符串,则默认去除左侧空白符
selectltrim('HelloWorld!')fromdual;--返回结果为'HelloWorld!' selectltrim('***+*HelloWorld!***+*','*+')fromdual;--返回结果为'HelloWorld!***+*'
(11)rtrim(x[,trim_string])从字符串右侧去除指定的所有字符串,原理同ltrim()
selectrtrim('HelloWorld!')fromdual;--返回结果为'HelloWorld!' selectrtrim('***+*HelloWorld!***+*','*+')fromdual;--返回结果为'***+*HelloWorld!'
(12)trim(trim_stringfromx)从字符串两侧去除指定的所有字符串 注意,ltrim()和rtrim()的截取集可以使多个字符,但trim的截取集只能有一个字符
selecttrim('*+'from'***+*HelloWorld!***+*')fromdual;
(13)nvl(x,value)将一个NULL转换为另外一个值,如果x为NULL,则返回value,否则返回x值本身
insertintostudentvalues(7,'猪猪',default,NULL); selectnvl(address,'北京市')fromstudent;
(14)nvl2(x,value1,value2),如果x不为NULL,返回value1,否则,返回value2
selectnvl2(address,'有地址','无地址')fromstudent;
(15)replace(x,search_string,replace_string),从字符串x中搜索search_string字符串,并使用replace_string字符串替换。并不会修改数据库中原始值
selectreplace('HelloWorld!','o','HA')fromdual;
(16)substr(x,start[,length])返回字符串中的指定的字符,这些字符从字符串的第start个位置开始,长度为length个字符;如果start是负数,则从x字符串的末尾开始算起;如果length省略,则将返回一直到字符串末尾的所有字符
selectsubstr('HelloWorld',3)fromdual;--返回结果为'lloWorld' selectsubstr('HelloWorld',-3)fromdual;--返回结果为'rld' selectsubstr('HelloWorld',3,2)fromdual;--返回结果为'll' selectsubstr('HelloWorld',-7,4)fromdual;--返回结果为'oWo'
2.数值函数
(1)abs(value)返回value的绝对值
selectabs(-10)fromdual;--返回结果为10
(2)ceil(value)返回大于等于value的最小整数
selectceil(2.3)fromdual;--返回结果为3
(3)floor(value)返回小于等于value的最大整数
selectfloor(2.3)fromdual;--返回结果为2
(4)trunc(value,n)对value进行截断,如果n>0,保留n位小数;n<0,则保留-n位整数位;n=0,则去掉小数部分
selecttrunc(555.666)fromdual;--返回结果为555,不加n时默认去掉小数部分 selecttrunc(555.666,2)fromdual;--返回结果为555.66 selecttrunc(555.666,-2)fromdual;--返回结果为500
(5)round(value,n)对value进行四舍五入,保存小数点右侧的n位。如果n省略的话,相当于n=0的情况
selectround(555.666)fromdual;--返回结果为556,不加n时默认去掉小数部分 selectround(555.666,2)fromdual;--返回结果为555.67 selectround(555.666,-2)fromdual;--返回结果为600
注意:
1.trunc和round用法类似,只不过trunc是硬生生截取,并不进行四舍五入,而round进行截取时四舍五入
2.都还可以对日期的截取,可以参考写的日期函数笔记
selectround(sysdate,'year')fromdual; selecttrunc(sysdate,'year')fromdual;
3.转换函数
将值从一种类型转换成另外一种类型,或者从一种格式转换为另外一种格式
(1)to_char(x[,format]):将x转化为字符串。format为转换的格式,可以为数字格式或日期格式
selectto_char('12345.67')fromdual;--返回结果为12345.67 selectto_char('12345.67','99,999.99')fromdual;--返回结果为12,345.67
(2)to_number(x[,format]):将x转换为数字。可以指定format格式
selectto_number('970.13')+25.5fromdual; selectto_number('-$12,345.67','$99,999.99')fromdual;
(3)cast(xastype):将x转换为指定的兼容的数据库类型
selectcast(12345.67asvarchar2(10)),cast('05-7月-07'asdate),cast(12345.678asnumber(10,2))fromdual;
(4)to_date(x[,format]):将x字符串转换为日期
selectto_date('2012-3-15','YYYY-MM-DD')fromdual
二.聚集函数
1.常用函数
(1)avg(x):返回x的平均值
selectavg(grade)fromsc;
(2)count(x):返回统计的行数
selectcount(name)fromsc;
(3)max(x):返回x的最大值
selectmax(grade)fromsc;
(4)min(x):返回x的最小值
selectmin(grade)fromsc;
(5)sum(x):返回x的总计值
selectsum(grade)fromsc;
2.对分组行使用聚集函数
对分组后的行使用聚集函数,聚集函数会统计每组中的值,对于每组分别统计后返回一个值。
示例
--按照职位分组,求出每个职位的最高和最低工资 selectjob,max(sal),min(sal)fromemp groupbyjob orderbyjob;
注意:
1.分组时select子句后边的列名必须与groupby子句后的列名一致,除非是聚合函数
selectdeptno,avg(sal)fromEMP;--错误,因为deptno不是聚集函数,也不是groupby后面跟的列名
2.不能使用聚集函数作为WHERE子句的筛选条件
selectdeptnofromempwhereavg(sal)>1000;--错误
3.分组后,需要使用条件进行筛选,则使用having过滤分组后的行,不能使用where,where只能放在groupby前面。
selectdeptno,avg(sal)fromempwheredeptno<>10 groupbydeptno havingavg(sal)>900;
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。