函数
一.单行函数
转换函数: to_char(), to_number(), to_date()
1.打印出“2018年12月22日 8:41:56”格式为当前系统的日期和时间。(注意使用双引号向日期中添加字符)。
select tochar(sysdate 'YYYY"年M"M"月"DD"日" HH:MI:SS ')from dual
2.格式化数字: 1234567.89 为 1,234,567.89
select tochar(1234567.89,'999,999,999.99')from dual
3.字符串转为数字时
1). 若字符串中没有特殊字符, 可以进行隐式转换:
select '1234567.89'+200 from dual
2). 若字符串中有特殊字符, 例如 '1,234,567.89', 则无法进行隐式转换, 需要使用 to_number() 来完成(括号中两种格式必须一致)
select tonumber('1,234,567.89','999.999.999.99')+100 from dual
4.对于把日期作为查询条件的查询, 一般都使用 to_date() 把一个字符串转为日期, 这样可以不必关注日期格式
select name,id_no,frist_date from student where tochar(frist_date,'yyyy-mm-dd')='2018-12-22'
5.查询每个月倒数第 3 天生日的学生的信息
select name,birthday from student where birthday=last_day(birthday)-2
二.多行函数
nvl,nvl2,case.....when.....then....else...end,decode
1.计算公司员工的年薪
注:nvl(commission_pct,0)如果commission_pct为空则输出0
select last_name,salary*12*(1+nvl(commission_pct,0)) year_sal from employees
2.查询部门号为 10, 20, 30 的员工信息, 若部门号为 10, 则打印其工资的 1.1 倍, 20 号部门, 则打印其工资的 1.2 倍, 30 号部门打印其工资的 1.3 倍数
1).使用case-when-then-else-end
注:当别名中有空格时一定要使用双引号
select name,department_id, salary,case department_id when 10 then salary*1.1
when 20 then salary*1.2
when 30 then salary*1.3
else salary end new_salary from employees where department_id in (10, 20, 30)
2).使用decode
select name,salary,department_id,decode(department_id,10,salary*1.1,
20,salary*1.2,
30,salaey*1.3)
end new_sal from employees where department_id in (10, 20, 30)