--1,打印出“2009年10月14日 9:25:40” 格式的当前系统的日期和时间
select to_char(sysdate,'yyyy"年"mm"月"dd"日" hh:mi:ss')
from dual;
--2,格式化数字:1234567.89为1,24,567.89
select to_char(1234567.89,'999,999,999.99')
from dual;
--3,字符串转为数字时
1)若字符串中没有特殊字符,可以进行隐式转换:
select '1234567.89' +100
from dual;
2) 若字符串中有特殊字符,例如‘1,234,567,89’,则无法进行隐式转换
需要使用to_number()来完成
select to_number('1,234,567.89','999,999,999,999.99') +100
from dual;
--4,对于把日期作为查询条件的查询,一般都使用to_date()把一个字符转换为日期
这样可以不必关注日期格式
select last_name,hire_date
from employees
where hire_date = to_date('1998-5-23','yyyy-mm-dd')
where to_char(hire_date,'yyyy-mm-dd')='1998-5-23'
--5,转换函数:to_char(),to_number(),to_date()
--6,查询每个月倒数第二天入职员工的信息
select last_name,hire_dare
from employees
where hire_date = last_day(hir_date)-1;
--7,计算公司员工的年薪
--错误写法:因为空值计算的结果还是空值
select last_name,salary*12*(1+commmission_pct) year_sal
from employees;
--正确写法
select last_name,salary*12*(1+nvl(commmission_pct,0)) year_sal
from employees;
--查询部门号为10,20,30的员工信息,若部门号为10,则打印其工资的1.1倍
20号的部门打印其工资为1.2倍,部门30打印其工资为1.3倍
--使用case-when-then-end
select last_name,department_id,salar,case separtment_id when 10 then salary * 1.1
when 20 then salary * 1.2
when 30 then salary * 1.3
end new_sal
from employees
where department_id(10,20,30);
--使用 decode
select last_name,department_id,salary,decode( department_id,10,salary * 1.1
20,salary * 1.2
30,dalary * 1.3
)new_sal
from employees
where department_id(10,20,30);