orcale数据库提供了很多内置的函数,使用内置函数能够很方便的对一般的数据进行操作。
第一部分、 字符串操作函数
1、将字符串转变成大写select upper('abcdet') from dual; -- string upper(string str)
2、将字符串转变成小写
select lower('ABCDE') from dual; -- string upper(string str)
3、将字符串的首字母大写
select initcap('ABCDE') from dual; -- string upper(string str)
4、获取字符串的长度
select length('ABCDE') from dual; -- int upper(string str)
5、将字符串首尾空格去掉
select trim(' ABCDE ') from dual; -- string upper(string str)
6、取字符串的子串
select substr('ABCDE',-3) from dual; -- string upper(string str,int m)
7、获取字符串中某个字段的索引位置
select instr('ABCDE','C') from dual;
-- string upper(string str1,string str2) = indexOf()
8、连接两个字符串
select concat('ABCDE','HHINGG') from dual;
-- string upper(string str,string str) == 'AB'||'BC'
9、获取字符串的子串
select substr('ABCDE',-3,3) from dual;
-- string upper(string str,int m,int n )
10、替代字符串中的字段
select replace('ABCDE','AB','ab') from dual;
-- string upper(string str,string str1,string str2)
11、字符串左填充
select lpad('ABCDE',10,'*') from dual;
-- string upper(string str,int ,string str1)
12、字符串右填充
select rpad('ABCDE',10,'*') from dual;
-- string upper(string str,int ,string str1)
第二部分、数值处理函数(正余弦函数除外)
1、数值小数点后四舍五入取值
select round(489.75467,2) from dual; --489.75
2、数值小数点前面四舍五入取值
select round(4898557,-2) from dual; --4898600
3、数值小数点后清除余额
select trunc(4898557,-2) from dual; --4898500
第三部分、日期处理函数
1、获取当前系统时间
select sysdate from dual; --2013/9/12 10:47:27
2、在当前的日期上面加或减去一天
select sysdate-1 from dual; --2013/9/11 10:48:29
3、在当前日期上增加若干月份
select add_months(sysdate,1) from dual; --2013/10/12 10:47:45
4、在当前日期上找到下一个星期一
select next_day(sysdate,'星期一') from dual; --2013/9/16 10:47:55
5、找到当前日期的月份的最后一天
select last_day(sysdate) from dual; --2013/9/30 10:48:02
6、获取两个日期中间间隔的月数
select months_between(sysdate, to_date('20090909','yyyymmdd')) from dual; --48.11
第四部分、转换函数(字符串,数值,日期之间的相互转换)
1、将日期等转换成字符 to_date(date.’format’)
select to_char(sysdate,'yyyy/mm/dd hh:Mi:SS') from dual;
--2013/09/12 10:51:00 标准的时间输出
select to_char(sysdate,'fmyyyy-mm-dd') from dual;
--2013-9-12, 自动的去掉9前面的0
select to_char(sysdate,'dd-mm-yyyy day') from dual;
--12-09-2013 星期四 加上星期
select to_char(sysdate,'yyyy/mm/dd hh24:mi:ss')from dual;
--2013/09/12 13:52:46 (将时间值转换成24小时制)
2、将数值转换成字符串
select to_char(35769646,'L999,999,999') from dual;
--¥35,769,646 (将数字转换成金额)L表示RMB,$表示美元符号
select to_char(35769646,'999,999,999') from dual;
-- 35,769,646 (将数字按照西方人的习惯输出)
select to_char(35769646,'9999,9999.99') from dual;
-- 3576,9646.00 (将数字按照中国人的数字习惯输出)
3、将字符串转换成日期
select to_date('2009/09/09','yyyy/mm/dd') from dual;
--2009/9/9
第五部分、通用函数
1、滤空函数 ,如果comm有值,没有值则输出0,同时也可以是其他的数字
select nvl(comm,0) from emp;
2、如果exp1与exp2的值相同输出0,不同输出exp1的值。
select nullif(exp1,exp2) from emp;
3、如果exp1等于null返回exp3,不等于null则返回exp2
select nvl2(exp1,exp2,exp3) from dual;
4、参数中遇到不为Null的则返回值
select coalesce(comm+sal,comm,0) from dual;