oracle学习之各种函数
单行函数:
upper(返回大写)
select Upper('abcde') FROM table;
lower(返回小写)
select Lower('ABCDE') FROM table;
initcap(首字母大写)
select initcap('hello') FROM table;
concat(拼接)
select concat('hello','abc') FROM table; == select ('hello'||'abc') FROM table;
substr和substrb(指定截取几到几的字符,substr按字符截取,substrb按字节截取)
select substr('好abcdef',1,3) FROM table;返回 好ab (按字符计,汉字英文都是一个字符)
select substrb('好abcdef',1,3) FROM table;返回 好,(按字节计,汉字一个占三,英文一个占一,按UTF-8算,其他编码也不同)
length和lengthb(返回长度,length按字符,lengthb按字节)
select length('好abcdef') FROM table; 返回7
select lengthb('好abcdef') FROM table;返回9
replace
select replace ('好abcdef','好','你好') FROM table;
instr(被搜索串,需搜索串,开始搜索位置,需搜索串出现的次数)
select instr('abceeeabcdddabc','abc',1,2) FROM table;返回7
Lpad左侧填充(原子符串,长度,扩充字符)将原子符串扩充成指定长度
select lpad('hello',10,'*') FROM table; 返回*****hello
Rpad右侧填充(原子符串,长度,扩充字符)
select lpad('hello',10,'*') FROM table;返回hello*****
trim(默认删除两端空格,leading删除头部空格,trailing删除尾部空格)
select trim(‘ hello ’) FROM table;返回hello
select trim('x' from 'xxxxabcxxx') FROM table;返回abc,可以删除两边的x,也可以只删除左边或右边,但是注意只能是单字符,多字符会报错
Round( ) 函数返回四舍五入的数值('数值','小数点后保留位数')
select round('123.123456','4') FROM table 返回123.1234
Mod()取余数
select mod('12','8') FROM table;返回4
Trunc()截取精度
select trunc('2017/08/08 15:11:58','yyyy')返回2017/08/08
Months_between()
select month_between('sysdate','sysdate') FROM table ;
Add_month()
select add_month('sysdate',1) FROM table ;
Next_day()
select next_day(sysdate,'星期一') FROMtable;下一个星期一
Last_day()
select last_day(sysdate) FROM table;当月最后一天
to_char()
select to_char(sysdate,'yyyy') FROM table;
select to_char(sysdate,'fmyyyy-mm-dd') FROM table;
select to_char(sal,'L999,999,999') FROM table;
select to_char(sysdate,'D') FROM table;//返回星期
to_number()
select to_number('13')+to_number('14') from dual; 返回27
to_date()
select to_date('20090210','yyyyMMdd') from rpmtperord; 返回2009/2/10
NVL()
select nvl(E1,E2) from emp; 如果E1为NULL,返回E2,否则返回E1
NVL2()
select nvl2(E1,E2,E3) from emp;如果E1为Null,返回E3,否则返回E2
COALESCE()
select coalesce(E1,E2,E3,E4,E5……) from emp;一次看E1,E2,E3。。。。。直到一个非null的值并返回这个值
CASE
select empno, ename, sal,
case deptno
when 10 then '财务部'
when 20 then '研发部'
when 30 then '销售部'
else '未知部门'
end部门
from emp;
DeCode()
select empno, ename, sal,
decode(deptno,
10, '财务部',
20, '研发部',
30, '销售部',
'未知部门')
部门
from emp; case和decode,case可以等于/大于等于,decode只能等于;case更简单,decode更高效;decode只能oracle使用
Count()
select count(*) from emp; 返回出现的次数
Avg(),max(),min(),sum()
avg()返回平均值,max()返回最大值,min()返回最小值,sum()返回累加值
group by()对结果进行分组
order by()对结果进行排序,默认升序,desc降序
having对group by()的结果进行在此筛选

浙公网安备 33010602011771号