--------------- 日期函数
select sysdate from dual
----员工入职 年限 日期相减 间隔的天数
select hiredate, (sysdate-hiredate)/365, floor((sysdate-hiredate)/365) from emp;
--- months_between() 日期间隔的 月数
select sysdate,months_between(sysdate,hiredate)/12,(sysdate-hiredate)/365 from emp
--- add_months()
select add_months('01-12月-2002',-1) from dual;
---- next_day() 表示下一个日期 (那一天 周几) [周天 - 周六]--[1-7]
select next_day(sysdate,1) from dual;
---last_day() 本月的最后一天
select last_day(sysdate) from dual;
----round ()
select round(sysdate,'month') from dual;
----round 默认支持 数字格式 不会自动转化 日期 和 14 没关系
select round(to_date('2000-02-15','YYYY-MM-dd'),'month') from dual;
select round(to_date('2000-02-16','YYYY-MM-dd'),'month') from dual;
---trunc()
select trunc(to_date('2019-11-20','YYYY-MM-dd'),'day') from dual;
--查询部门是二十号部门,或者名字第三位是O 或者工资大于5000的员工的入职年限
select floor((sysdate-hiredate)/365) 入职年限,ename,empno from emp where deptno=20 or instr(ename,'O')=3 or sal>5000;
--查询入职日期大于30 年的,并且奖金不是null,员工姓名和 job
select ename, job from emp where floor((sysdate-hiredate)/365)>35 and comm is not null
--查询入职时间加上12年之后大于47年的员工
select * from emp where (floor((sysdate-hiredate)/365)+12)>47
----- 转化函数
------ to_date 将字符串 转成date
select to_date('2019-11-20','yyyy-mm-dd') from dual; -- mm MM 都表示月份
select to_date('2019-5月-20','yyyy-mon-dd') from dual; --- 5月 mon month
select to_date('2019-05-20 11:23:13 ','yyyy-mm-dd hh:MI:ss') from dual; --12 小时制
select to_date('2019-05-20 13:23:13','yyyy-mm-dd hh24:MI:ss') from dual; --24 小时制
select to_date('2019-05-20 13:23:13','yyyy-mm-dd hh24:MI:ss ') from dual;
-- to_char date--char
select sysdate from dual;
select substr( to_char(sysdate,'yyyy-mm-dd hh-MI-ss'),1,10 )from dual
select to_char(sysdate,'yyyy"年"mm"月"dd"日"') from dual -- 格式中 “” 里面的内容原样输出 不参与转化
select to_char(sysdate,'yyyy"year"mm"month"dd"day"') from dual
select to_char(sysdate,'yyyy"year"mm"month"dd"day" ddspth DD') from dual
---2月份入职的人 ?
select * from emp where to_char(hiredate,'mm')='02';
---to_char number--char
select to_char(123.00 ,'999.999') from dual ---9 格式 数值不够 补空格 小数点后 补 0
select to_char(-123.00,'000,000.000')from dual -- -0格式 数值不够 补0 小数点后 补 0
-- to_char 数字 转字符串 每个默认加有 一个空格
select to_char(123.00,'999.999') from dual where trim(to_char(123.00,'999.999'))= ' 123.000'
---员工工资
select to_char(123.00,'L99,999.000')from emp
select to_char(123.00,'$99,999.000')from emp
---- to_number 将字符串 转成 数字
select to_number('123.66' ,'000,000.000') from dual;
select to_number('123.66' ,'999,999.999') from dual;
----通用函数
----- nvl (1,2) 表达式1 为空 则返回表达式2 否则 返回表达式1
---总工资
select sal+comm 总工资 from emp;
select sal+nvl(comm,0) 总工资 from emp;
select nvl(null,10) from dual;
select nvl('1','10') from dual;
----- nvl2 (1,2,3) 表达式1 为空 返回表达式 3 否则是 2
select nvl2(comm,'不为空','为空') from emp
select nvl2(comm,sal+comm,sal) from emp
---nullif 判断两个值是否相等 相等 返回 空 如果不相等 返回表达式1 数据类型 得一致
select nullif('123','1231') ,nullif('1','2'),nullif('',null) from dual;
-- coalesce 返回第一个不为空的值
select coalesce(1,2,3,3),coalesce(null,0,1) from dual;
----- if else ---
case 表达式
when 值1 then
when 值1 then
else
end
------ 等值判定
select case 4
when 1 then '壹'
when 2 then '贰'
when 3 then '叁'
else '瞎写'
end 困 from dual;
select *from emp
--- CLERK 办事员 SALESMAN 销售 MANAGER 经理 ANALYST 分析师 PRESIDENT 董事长
select ename,
case job
when 'CLERK' then '办事员'
when 'SALESMAN' then '销售'
when 'MANAGER' then '经理'
when 'ANALYST' then '分析师'
when 'PRESIDENT' then '董事长'
end 具体工作,hiredate
from emp;
---- 不等值
select case
when sal<1000 then '屌丝'
when sal<2000 then '穷鬼'
when sal<3000 then '小康'
else '刷礼物'
end 困 from emp;
------ 工资 >2000*0.8 >3000 0.7 其他 不扣 实际月薪(税后 包含奖金)
----- decode(表达式 , 值1 , 返回值1 ,值2 , 返回值2 .。。,返回值n(默认返回值) ) --只能做等值
select *from emp ---10 财务部 20 销售部 30 技术部
select emp.* , decode(deptno,10,'财务部',20,'销售部',30,'技术部') from emp
select emp.* , decode(deptno,10,'财务部',20,'销售部','没人管') from emp