单行函数:只对针对一行进行, 返回一行记录
字符串相关函数:
1 lower 小写, upper 大写, initcap 单词的首字母大写
1 | select lower('HELLO WORLD') "小写", upper('Hello world') "大写", initcap('hello world') "首字母大写" from dual; |
2 concat(连接符||)
1 | select concat('hello ','world') from dual; |
3 substr(str,pos,len)截取字符串
1 | select substr('helloworld',1,3), substr('helloworld',1), substr('helloworld',-3) from dual; |
4 instr(str, substr):判断substr是否在str中存在, 若存在返回第一次出现的位置, 若不存在则返回0
1 | select instr('hello llo', 'llo'), instr('hello llo', 'ow')from dual; |
5 lpad和rpad–l®pad(str, len, ch):返回len长度的字符串, 如果str不够len的话, 在左(右)填充ch这个字符
1 | select lpad('aaaa', 10, '$'), rpad('aaaa', 10, '#') from dual; |
6 trim:去掉首部和尾部的空格,中间的空格不去掉
1 | select 'aaa'||trim(' hello world ')||'bbb' from dual; |
7 replace(str, old, new):将str字符串中的old字符串替换成new字符串
1 | select replace('hello world','llo','yy') from dual; |
8 length和lengthb
1 | select length('hello world') 字符数, lengthb('hello world') 字节数 from dual; |
数值函数
1 round: 四舍五入
1 | select round(45.926, 2) 一, round(45.926, 1) 二, round(45.926, 0) 三, round(45.926, -1) 四, round(45.926, -2) 五 from dual; |
trunc: 截取
1 | select trunc(45.926, 2) 一, trunc(45.926, 1) 二, trunc(45.926, 0) 三, trunc(45.926, -1) 四, trunc(45.926, -2) 五 from dual; |
2 mod
1 | select mod(1600, 300) from dual; |
3 ceil:向上取整 floor:向下取整
1 | select ceil(121/30), floor(121/30) from dual; |
转换函数:
1 to_char和to_number
1 | --把薪水转换为本地货币字符型 |
2 to_char 与 to_date
1 | --显示 "yyyy-mm-dd hh24:mi:ss 今天是 星期几" |
查询1981-11-17日入职的员工信息:
1 | select * from emp where to_char(hiredate, 'YYYY-MM-DD')='1981-11-17'; |
oracle的隐式转换和显示转换:
1 | select 11+'22' from dual; |
时间和日期函数:
显示当前的系统日期
1 | select sysdate from dual; |
显示当前的系统日期显示到秒
1 | select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from dual; |
显示当前日期星期几
1 | select to_char(sysdate, 'day') from dual; |
显示昨天,今天,明天–oracle日期型+1代表加一天
1 | select sysdate-1 昨天,sysdate 今天,sysdate+1 明天 from dual; |
计算员工工龄 可以按日,周,月,年 日期差减方法
1 | select empno,ename,sysdate-hiredate 日,(sysdate-hiredate)/7 周,(sysdate-hiredate)/30 月,(sysdate-hiredate)/365 年 from emp; |
日期函数 months_between add_months last_day next_day
1 | select empno, ename, months_between(sysdate,hiredate), (sysdate-hiredate)/30 月 from emp; |
add_months:增加月份
1 | select add_months(sysdate, 2) from dual; |
求明年的今天
1 | select add_months(sysdate,12) from dual; |
last_day:最后一天–指定日期所在月份的最后一天
1 | select last_day(sysdate) from dual; |
next_day:求指定日期的下一个星期几
1 | select next_day(sysdate, '星期一') from dual; |
round、trunc 对日期型数据进行四舍五入和截断
1 | select round(sysdate, 'month'), round(sysdate, 'year') from dual; |