您的位置:首页 > 数据库 > Oracle

oracle 函数集 整理

2010-08-04 15:09 295 查看
--------------------
--|    字符函数    |
--------------------

--字符串连接 asssbsss
Select concat('asss','bsss') from dual;
select 'a'||'b' from dual;
--单词首字母大写 Red Hat
select initcap('red hat') from dual;
--字符串截取 结果为 def
Select substr('abcdef',length('abcdef')-2) from dual;
Select substr('abcdef',-3,3) from dual;
--取字符串长度 6
select length('123456') from dual;
--字符替换 111111
select replace('aaaaaa','a','1') from dual;
--查找子串的位置 8
select instr('hello world','or') from dual;
--左侧填充* 总长度为8 结果为 ***muger
select lpad('muger',8,'*') from dual;
--右侧填充 muger***
select rpad('muger',8,'*') from dual;
--过滤首尾空格
select trim('   muger   ') from dual;

--////////////单行函数//////////////////

--------------------
--|    数值函数    |
--------------------

--四舍五入
select round(456.6,-2) from dual; --500
select round(412.316,2) from dual; --412.32
--取余数
select mod(33,5) from dual; --33mod5 = 3
--取下界 直接舍掉 不四舍五入
select trunc(477.13,-2) from dual; --400

--------------------
--|    日期函数    |
--------------------

--计算相差月份 结果单位为(月)sysdate为当前系统时间
select months_between(sysdate,sysdate-31) from dual; -- 1
--日期月份加 n
select add_months(sysdate,1) from dual; --sysdate=2010-8-3 17:10:29 返回结果为 2010-9-3 17:10:29
--得到下一个 星期三 那天此时的日期
select next_day(sysdate,3) from dual; --
select next_day(sysdate,'星期三') from dual;--同上
--获取当月最后一天 此时的日期
select last_day(sysdate) from dual;

--------------------
--|    转换函数    |
--------------------

--日期到字符串的转换
select to_char(sysdate,'yyyy') from dual; --取年'yyyy' 月 'mm' 日 'dd' 时 'hh' 分 'mm' 秒 'ss'
select to_char(sysdate-20,'fmyyyy-mm-dd') from dual; --单月单日不补0 例如 2010-8-18 而不是 2010-08-18
select to_char(45641313,'L999,999,999') from dual;--  ¥45,641,313
select to_char(sysdate-20,'d') from dual;--返回星期

--字符串转数字
select to_number('13')+to_number('14') from dual; --27

--字符串转日期
Select to_date('20090210','fmyyyyMMdd') from dual; --2009210

--------------------
--|    通用函数    |
--------------------

--如果为NULL 用0替换
select nvl(null,0) from dual; --0
--如果表达式exp1与exp2的值相等则返回null,否则返回exp1的值
select nullif(12,12) from dual; --null
--exp1是null 返回exp3 否则返回exp2
select nvl2(3,12+5,8) from dual; --17
--依次考察各参数表达式,遇到非null值即停止并返回该值。
select coalesce(null,null,null,2,null) from dual; --2
--case表达式
select a,b,
case c
when 10 then '十'
when 20 then '二十'
when 30 then '三十'
else '未知'
end 数字
from
(select 20 as a,35 as b,30 as c from dual);
--decode表达式 --结果case
select a,b,
decode(c,
10,'十',
20,'二十',
30,'三十',
'未知')
数字
from
(select 20 as a,35 as b,30 as c from dual);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: