1、单行函数数据库
一、字符函数函数
1.一、concat(str1,str2) 字符串拼接函数code
Select concat(‘hello’,’world’) from dual;orm
等价于索引
Select ‘hello’ || ‘world’ from dual;字符串
1.二、initcap(str)将每一个单词首字母大写,其它字母小写string
Select initcap(‘hello world’) from dual; è’Hello World’;it
Select initcap(‘HELLO WORLD’) from dual è’Hello World’;ast
1.三、instr(x,find_string[,start][,occurrence]) 返回指定字符串在某字符串中的位置,能够指定搜索的开始位置和返回第几回搜索出来的结果(这个位置是从1开始算起)form
Select instr(‘Hello World’,’o’) from dual;è 5
Select instr(‘Hello World’,’o’,6) from dual;è8
Select instr(‘Hello World’,’o’,1,2) from dual;è8
Select instr(‘Hello World’,’c’) from dual; è0
Instr()函数能够充当模糊查询,当数量十万以上时,效果才慢慢的体现出来,数量越大,效果越明显,并且,当查询的字段加上索引后,instr查询的更快
Instr(字段,’xx’)>0 è 字段 like ‘%xx%’
Instr(字段,’xx’)=1 è 字段 like ‘xx%’
Instr(字段,’xx’)=0 è 字段 not like ‘%xx%’
1.四、length(str) 返回表达式中的字符数
Select length(‘abc’) from dual; ==》3
Select length(1234) from dual; è4
1.五、lower(str) 将字符串转换为小写
Select lower(‘Hello World’) from dual; èhello world
1.六、upper(str) 将字符串转换为大写
Select upper(‘hello world’) from dual; èHELLO WORLD
1.七、lpad(str,width[,pad_string]) 当字符串长度不够时,左填充补齐,能够指定补齐时用什么字符补齐,若不指定,则以空格补齐
Selece lpad(‘hello world’,20) from dual; è hello word
Select lpad(‘hello world’,20,’*’) from dual;è*********hello world
1.八、rpad(str,width[,pad_string]) 同上
1.九、ltrim(x[,trim_string]) 从字符串左侧去除指定的全部字符串,若没有指定去除的字符串,则默认除去左侧空白符
Select ltrim(‘ hello world ‘) from dual; èhello world
Select ltrim(‘****hello world****’) from dual;èhello world****
1.十、rtrim(x[,trim_string]) 从字符串右侧去除指定的全部字符串,原理同ltrim()
1.十一、trim(trim_string from x) 从字符串中两侧去除指定的全部字符串
Select trim(‘*’ from ‘***++hello world***++’) from dual;è++hello world++
注意,ltrim()和rtrim()的截取集能够使多个字符,但trim的截取集只能有一个字符
select trim('*+' from '***+*Hello World!***+*') from dual; è报错,’*+’ 是两个字符
1.十二、trim(str) 去除两边的空格
Select trim(‘ abc ‘) from dual; èabc
1.1三、nvl(x,value) 将一个null转换为另外一个值,若是x的值为null,则返回value,不然返回x值自己
Select nvl(x,’值为空’) from dual;
1.1四、nvl2(x,value1,value2) 若是x不为空,返回value1,不然返回value2
1.1五、replace(x,search_string,replace_string) 从字符串x中搜索search_string字符串,并使用replace_string 字符串替换,并不会修改数据库中的原始值
Select replace(‘hello world’,’hello’,’hi’) from dual; èhi world
1.1六、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
二、数值函数
2.一、abs(value) 返回value的绝对值
Select abs(-10) from dual; è10
2.二、ceil(value) 返回大于等于value的最小值
Select ceil(2.3) from dual; è3
2.三、floor(value) 返回小于等于value的最大整数
Select floor(2,3) from dual; è2
2.四、trunc(value,n) 对value进行截断,若是n>0,保留n位小数;n<0,则保留-n 位整数位; n=0,则去掉小数部分
Select trunc(555.666) from dual; ==》555
Select trunc(555.666,2) from dual; è555.66;
Select trunc(555.666,-2) from dual; è500
三、转换函数(将值从一种类型转换成另外一种类型,或者从一种格式转换成另一种格式)
3.一、to_char(x[,format]):将x转换为字符串,format为转换的格式,能够为数字格式或日期格式
Select to_char(‘1234.56’) from dual; è1234.56
Select to_char(‘12345.67’,’99,999.99’) from dual; è12,345.67
3.二、to_number(x[,format]):将x转换为数字,能够指定format格式
Select to_number(‘970.13’)+25.5 from dual;è995.63
Select to_number(‘-$12,345.67’,’$99,999.99’) from dual; è -12345.67
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;è 12345.67 2007/7/5 12345.68
3.四、to_date(x[,format]):将x字符串转换为日期
Select to_date(‘2018/02/15 11:10:30’,’yyyy/MM/dd hh24:mi:ss’) from dual;=> 2018/2/15 11:10:30
四、通用函数
4.一、decode用法
decode(字段或字段的运算,值1,值2,值3)
这个函数运行的结果是,当字段或字段的运算的值等于值1时,该函数返回值2,不然返回值3
固然值1,值2,值3也能够是表达式。
4.二、sign用法
sign()函数根据某个值是0、正数仍是负数,分别返回0、一、-1
2、汇集函数
一、经常使用函数
1.一、avg(x):返回x的平均值
Select avg(age) from sc;
1.二、count(x)返回统计的行数
Select count(name) from sc;
1.三、max(x):返回x的最大值
Select max(grade) from sc;
1.四、min(x):返回x的最小值
Select min(grade) from sc;
1.五、sum(x):返回x的总计值
Select sum(grade) from sc;
二、对分组使用汇集函数
对分组后的行使用汇集函数,汇集函数会统计每组中的值,对于每组分别统计后统计后按返回一个值。
2.一、分组时select子句后面的列名必须与group by子句后的列名一致,除非是聚合函数
Select deptno,avg(sal) from emp;è错误,由于deptno不是汇集函数,也不是group by 函数
2.二、不能使用汇集函数做为where子句的筛选条件
Select deptno from emp where avg(sal)>1000;è 错误
2.三、分组后,须要使用条件进行筛选,则使用having过滤分组后的行,不能使用where,where只能放在group by前面
Eg:select deptno, avg(sal) from emp where deptno<>10 group by deptno having avg(sal)>900