oracle数据库常用的一些函数

函数:数值函数,字符函数,日期函数,转换函数
数值函数:四舍五入,取整函数,常用计算,三角函数

select round(12.34),round(12.34,1),round(12.34,-1) from dual;

取整 ceil(m)向上取整,floor(m)向下取整

select ceil(12.34),floor(12.34) from dual;

abs(m)取绝对值

select abs(12.34),abs(-12.34) from dual;

mod(m,n)m对n取余 m与n有一个为空,则返回空

select mod(5,3) from dual;
select mod(9,null) from dual;

power(m,n) 返回m的n次幂 有一个为空返回空

select power(2,3) from dual;
select power(2,null) from dual;

sqrt(m)返回m的平方根

select sqrt(16) from dual;

三角函数 传入的值为弧度值 sin(m) asin(m) cos(m) acos(m) tan(m)

select sin(30) from dual;
select asin(0.5) from dual;
select tan(1.2) from dual;
select atan(1.2) from dual;

字符函数:大小写转换函数initcap(‘首字母转大写函数’)
截取字符串函数substr(‘char’,m,n) n省略则是从m位置截取到字符串结束位置 m为0,从开始位置截取;m为负数,从字符串结尾截取
查询字符串长度 length(‘string’) 注意:由空格的话空格也占长度

select upper('abCDe'),lower('abCDe'),initcap('abCDe') from dual;
select substr('abcde',0,3) from dual;
select length('asdfg ') from dual;

字符串连接函数 concat(char1,char2) 与|| 操作符一致
select concat(‘abc’,’def’) from dual;
去除子串函数 trim(c2 from c1) 从c1中去除c2 只能去除一个字符 首位或者末尾

select trim('s' from 'asdfg') from dual;
select ltrim('aabbaa','a') from dual;--从左边去除重复的
select rtrim('aabbaa','a') from dual;--从右边去除重复的
select length(trim(' addd ')) from dual;--如果里面只有一个字符,意思是去除首尾空格

替换函数 replace(stringa,stringb,stringc) 把stringa里stringb用stringc替换,如果stringc省略则用空格替换

select replace('asdfga','a','A') from dual;

日期函数:系统时间(默认格式:DD-MM-YY),日期操作add_months(m,n)在m月份上添加n月,如果n为小数,自动截取整数部分,n为负数,减去相应月份

select sysdate from dual;
select add_months(sysdate,1.8) from dual;

next_day(date,char) 如果char的值是星期一,返回date指定日期的下周一是哪天

select next_day(sysdate,'星期一') from dual;

last_day(date)返回date所在月的最后一天

select last_day(sysdate) from dual;

months_between(date1,date2)返回两个日期相差的月份

select months_between('2-5月-18','7-1月-18') from dual;

extract(date from datetime) 获取日期的年月日

select extract(day from sysdate),extract(month from sysdate),extract(year from sysdate) from dual;
select extract(hour from timestamp '2018-1-2 18:10:22'),extract(minute from timestamp '2018-1-2 18:10:22'),extract(second from timestamp '2018-1-2 18:10:22') from dual;
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
select sysdate from dual;

转换函数:日期转换成字符的函数,字符转换成日期的函数,数字转换成字符的函数,字符转换成数字的函数
to_char(date,fmt) 日期转字符串 日期,要转换的格式 日期默认格式DD-MON-RR

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate,'yyyy-mm-dd hh12:mi:ss') from dual;

注意:to_date()按照系统默认格式显示日期,并不是我们后面定义的日期格式

select to_date('2018-2-3 20:15:30','yyyy-mm-dd hh24:mi:ss') from dual;

数字转换成字符的函数:to_char(num,fmt)
9:显示前面的数字并忽略前面的0
0:显示数字,位数不足,用0补齐
.或D:显示小数点
,或G:显示千分位
$:美元符号
S:加正负号(前后都可以)

select to_char(11123568987.0,'$999,999,999,999,999.000000') from dual;
select to_char(-12345678910.123456,'S999,999,999,999,999') from dual;

字符转数字 to_number(char,fmt) 如果fmt省略,则字符是什么格式就转什么格式

select to_number('$123,256.123','$999,999.000') from dual;
select to_number(123256.123) from dual;

在查询中使用函数
假设有一张user表,表中存了员工的身份证号cardid,获取员工的生日

select substr(cardid,7,8) from user;

猜你喜欢

转载自blog.csdn.net/liyufu318/article/details/80114454