Oracle学习之函数

1.字符函数
Upper() 大写
select upper(name) from liutesttable; ------'LIU'
Lower() 小写
select lower(name) from liutesttable; -------'liu'
Initcap() 首字母大写
select initcap(name) from liutesttable; -------'Liu'
Concat() 将字符或列值连接
select concat(name,age) from liutesttable; -----'liu33'
select name || age from liutesttable; -----'liu33'
select 'a'||'bc' from liutesttable; -----'abc'
Substr() 截取字符串
select substr(name,0) from liutesttable; ------'liu'
select substr(name,0,2) from liutesttable; -------'li'
Lenght() 字符串长度
select length(name) from liutesttable; ------- 3
Replace() 替换
select replace(name,'i','*****') from liutesttable; ------'l*****u'
Instr() 在字符串中首次出现的位置
select instr(name,'i') from liutesttable; ------ 2
Lpad() 字符长度小于指定长度时在左侧填充指定的字符,否则从开始位置截取指定长度的字符串
select Lpad(name,6,'u') from liutesttable; -------'uuuliu'
select Lpad(name,2,'u') from liutesttable; -------'li'
Rpad() 同Lpad()
Trim() 过滤收尾空格
select trim(' Liu Lan Qing ') from liutesttable;---------'Liu Lan Qing'

2.数值函数
Round() 四舍五入,可指定保留几位小数
select round(144.7225) from liutesttable; ----- 145
select round(144.7225,2) from liutesttable; ------ 145.72
select round(144.7225,-2) from liutesttable; ------ 100
Mod() 取余数
select mod(10,3) from liutesttable; ----- 1
Trunc() 取整数舍去小数部分,可指定保留几位小数。(还可用于指定元素而截去的日期值)
select trunc(144.2225) from liutesttable; --------144
select trunc(144.2225,3) from liutesttable; --------144.222
select trunc(144.2225,-2) from liutesttable; --------100


3.日期函数
Months_between() 比较两个日期的差
select months_between(sysdate,add_months(sysdate,1)) from liutesttable; ------- -1
Add_Months() 月份添加
select add_months(sysdate,1) from liutesttable; ----- 21-3月-11
Next_day() 下一个指定星期的日期
select next_day(sysdate,'星期一') from liutesttable; -----28-2月 -11 (当日:21-2月 -11)
Last_day() 某月的最后一天的日期
select last_day(sysdate) from liutesttable; ------28-2月 -11 (当日:21-2月 -11)
Greatest() 取最大时间值,Least() 取最小时间值
systimestamp 获取当前时间值
select systimestamp from liutesttable; -----21-2月 -11 03.47.59.600000000 下午 +08:00
sysdate 获取当前时间值
select sysdate from liutesttable; --------21-2月 -11
current_date获取当前时间值
select current_date from liutesttable; ----21-2月 -11


4.转换函数
To_char() 转换成char类型
select to_char(123456) from liutesttable; ------ '123456'
select to_char(123456789,'L999,999,999') from liutesttable; ----- ¥123,456,789 (位数一致)
select to_char(sysdate,'D') from liutesttable; ------ 2 星期一 (日一二三四五六==1234567)
select to_char(sysdate,'yyyy') from liutesttable; ------ 2011
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from liutesttable; -----(注意:分钟MI)
To_number 转换为number类型
select to_number('123')+to_number('123') as aa from liutesttable; ------ 246
To_date 转换成date类型
select to_date('22-2月 -11') from liutesttable; --------22-2月 -11
select to_date('20110102','yyyy/mm/dd') from liutesttable; --------02-1月 -11 (格式问题待研究)


5.通用函数
nvl() 类似mssql中的isnull()
select nvl(remarks,'我被删除了') from liutesttable; -----------我被删除了
nvl2() 类似三元运算符
select nvl2(remarks,'Remarks不为空','Remarks为空') from liutesttable; ------Remarks为空
nullif(expr1, expr2) 相等返回NULL,不等返回expr1
select nullif(age,22) from liutesttable; ------ 33
select nullif(age,33) from liutesttable; ------ null
case 表达式 同mssql
decode() 类似于case表达式
select decode(age,22,'我22岁了',33,'我33岁了') from liutesttable;
Coalesce() 查找第一个非空值
select COALESCE(name,remarks) from liutesttable ---- 'liu' (列类型要一致)

posted @ 2011-03-09 15:09  留下  阅读(300)  评论(0编辑  收藏  举报