单行函数:
1.字符函数:
select lower('Hello World') 转小写,upper('Hello World') 转大写,initcap('hello world') 首字母大写 from dual;
select substr('Hello World',4,3) 子串 from dual; --substr('xxx',4,3)从第4位开始截取字符串,取3个字符。
select length('北京') 字符,lengthb('北京') 字节 from dual; --length 字符数; lengthb 字节数
select instr('Hello World','ll') 位置 from dual; --instr(a,b) 在a中,查找b
select lpad('abcd',10,'*') 左填充,rpad('abcd',10,'*') 右填充 from dual; --lpad('abcd',10,'*') 左填充,填满10位
select trim('H' from 'Hello WorldH') from dual; --trim('x' from 'xxxxx') 去掉字符串两端的指定字符(可以是空格)
select replace('Hello World','l','*') from dual; --replace('Hello World','l','*') 替换
2.数值函数
select round(45.926,2) 一,round(45.926,1) 二,round(45.926,0) 三,round(45.926,-1) 四,round(45.926,-2) 五 from dual; --四舍五入
select trunc(45.926,2) 截断 from dual; --保留2位小数,后面的直接截断
select mod(1600,300) 求余 from dual; --求余
3.日期函数
MySql中的date表示日期,datetime表示日期和时间
Oracle中的date表示日期和时间
select sysdate from dual; --查询当前日期时间
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; --日期时间根据指定格式转换成字符串
select (sysdate-1) 昨天,sysdate 今天,(sysdate+1) 明天 from dual; --date与数字加减,单位是天。两个日期相减返回相差天数
select months_between(sysdate,hiredate) 相差月份 from emp; --months_between() 两个日期相差的月份
select add_months(sysdate,-10) from dual; --add_months() 几个月后的日期,负数表示几个月前
select last_day(sysdate) from dual; --last_day(sysdate) 本月最后一天
select next_day(sysdate,'星期五') from dual; --下一个星期五
select round(sysdate,'month'),round(sysdate,'year') from dual; --日期的四舍五入
select trunc(sysdate,'month'),trunc(sysdate,'year') from dual; --日期的截断
4.转换函数
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss"今天是"day') from dual; --日期转字符串。 格式中的双引号的内容原样输出
select to_char(sal,'L9,999.99') from emp; --数字转字符串。 L表示本地货币符号
--to_number() 字符串转数字; to_date() 字符串转日期
5.通用函数
nvl() 滤空函数。 --nvl(列名,0) 某条记录的指定列的值如果为null,那么返回0
select sal*12+nvl2(comm,comm,0) from emp; --nvl2(a,b,c) 当a=null的时候,返回c;否则返回b
select nullif('abc','abc') 值 from dual; --nullif(a,b) 当a=b的时候,返回null;否则返回a
select comm,sal,coalesce(comm,sal) from dual; --coalesce() 从左到右,找到第一个不为null的值
6.条件表达式
--case表达式是SQL99标准的语法; decode函数是Oracle自己的语法。
select ename,job,sal,
case job when 'PRESIDENT' then sal+1000
when 'MANAGER' then sal+800
else sal+400
end 涨后
from emp; --case表达式
--如果job是'PRESIDENT'那么sal+1000;否则如果job是'MANAGER'那么sal+800
select ename,job,sal,
decode(job,'PRESIDENT',sal+1000,
'MANAGER',sal+800,
sal+400) 涨后
from emp; --decode函数
日期格式(转换函数):
数字格式(转换函数):