单行函数

单行函数:只对针对一行进行, 返回一行记录

字符串相关函数:

1 lower 小写, upper 大写, initcap 单词的首字母大写

1
select lower('HELLO WORLD') "小写", upper('Hello world') "大写", initcap('hello world') "首字母大写" from dual;

2 concat(连接符||)

1
2
3
4
5
6
7
select concat('hello ','world') from dual;
**注意: concat函数只能连接两个字符串, 若想连接三个的话只能嵌套调用:
select concat(concat('hello ','world'), ' nihao') from dual;
select 'hello ' || 'world ' || 'nihao' from dual;
**注意: || 可以连接多个字符串, 建议使用||来连接字符串.

**总结: concat只能用于两个字符串的连接, ||可以用于多个字符串的连接, 在使用的使用建议尽量的使用||.

3 substr(str,pos,len)截取字符串

1
2
select substr('helloworld',1,3), substr('helloworld',1), substr('helloworld',-3) from dual;
**总结:pos是从1开始的, 若len为0表示从pos开始, 截取到最后, 若pos为负数, 表示从末尾倒数开始截取,

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
2
3
select 'aaa'||trim('  hello world  ')||'bbb' from dual;  
trim(c from str):去掉str中的c字符
select trim('x' from 'xxxxxhello worldxxxxx') from dual;

7 replace(str, old, new):将str字符串中的old字符串替换成new字符串

1
select replace('hello world','llo','yy') from dual;

8 length和lengthb

1
2
3
4
select length('hello world') 字符数, lengthb('hello world') 字节数 from dual;
select length('哈喽我的') 字符数, lengthb('哈喽我的') 字节数 from dual;
注意:对于length函数一个汉字是一个字符, 对于lengthb函数,一个汉字占两个,
这两个函数对于普通字符串没有什么区别.

数值函数

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
3
4
--把薪水转换为本地货币字符型
select empno,sal,to_char(sal,'L9,999') from emp;
--把上述某个结果转回数值型
select to_number('¥2,975','L9,999') from dual;

2 to_char 与 to_date

1
2
3
4
--显示 "yyyy-mm-dd hh24:mi:ss 今天是 星期几"
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss "今天是" day') from dual;
--将上述输出字符串反转回日期
select to_date('2017-12-04 01:12:48 今天是 星期一', 'yyyy-mm-dd hh24:mi:ss "今天是" day') from dual;

查询1981-11-17日入职的员工信息:

1
2
select * from emp where to_char(hiredate, 'YYYY-MM-DD')='1981-11-17';
select * from emp where hiredate = to_date('1981-11-17', 'YYYY-MM-DD');

oracle的隐式转换和显示转换:

1
2
3
4
5
6
7
8
9
10
11
select 11+'22' from dual;
select 11+to_number('22') from dual;
对于select 11+'22' from dual; 会做隐式转换, 将'22'转换成22

select '11' || 22 from dual;
select '11' || to_char(22) from dual;
对于select 11+'22' from dual; 会做隐式转换, 将22转换成'22'

select 11+'1a' from dual; --报错, 1a不是数字, 所以不能转

**总结: 当没有明确转换函数的时候, 如果类型不一致, 会进行隐式转换, 隐式有一个前提, 它必须能转换, 但应尽量避免隐式转换。

时间和日期函数:

显示当前的系统日期

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
2
select round(sysdate, 'month'), round(sysdate, 'year') from dual;
select trunc (sysdate, 'month'), trunc(sysdate, 'year') from dual;