Select * From Emp Where Comm>Sal;
--处理null的单行函数
--nvl(exp1,exp2)单行函数
Select Sal,Comm,Nvl(Comm,0)+Sal Sum,Ename From Emp;
--nvl2(exp1,exp2,exp3)单行函数
Select Empno,Nvl2(Comm,Sal+Comm,Sal) From Emp ;
--order by
Select Ename,Hiredate From Emp Order By Hiredate Desc;
--依据dual表做数据处理
--Declare Str='abcde';
--Select Substr(Str,1,Length(Str)-2) From Dual;
Select Substr('abcde',1,Length('abcde')-2) From Dual;
Select Substr('abcde',Length('abcde')-2) From Dual;
--月份之差
Select Months_Between(Sysdate,Hiredate) From Emp;
--从27年前开始 之后雇员信息
Select * From Emp Where Months_Between(Sysdate,Hiredate)<12*27;
--查询25年前的雇员
Select * From Emp Where Hiredate<=Add_Months(Sysdate,-25*12);
-- fm将06转化为6,10还是为10
Select * From Emp Where To_Char(Hiredate,'fmmm')='12';
--将1999-02-03转化为1999-2-3
Select To_Char(Hiredate,'fmyyyy-mm-dd hh:mm:ss') From Emp;
--查询1981年最后两个月入职的
Select * From Emp
Where
Months_Between(To_Date('1981-12-31','yyyy-mm-dd'),Hiredate)<2
And
Months_Between(To_Date('1981-12-31','yyyy-mm-dd'),Hiredate)>0;
--使用绝对值的话会查询到1982年的
Select * From Emp
Where
Abs(Months_Between(To_Date('1981-12-31','yyyy-mm-dd'),Hiredate))<2;
--计算年龄
Select To_Number(To_Char(Sysdate,'yyyy'))-1992 From Dual;
--Select to_char(Hiredate,'yyyy') From Emp;
--查询工龄
Select Ename,
To_Number(To_Char(Sysdate,'yyyy'))-To_Number(To_Char(Hiredate,'yyyy')) WorkAge
From Emp;
Select Empno,Hiredate,Round(Months_Between(Sysdate,Hiredate)/12,2) Workage From Emp;
--找出每个月倒数第三天受雇的员工(如:2009-5-29)
Select * From Emp Where Last_Day(Hiredate)-2=Hiredate;
--找出25年前雇的员工
Select * From Emp Where Months_Between(Sysdate,Hiredate)>12*25;
--找出姓名为5个字母的员工
Select * From Emp Where Length(Ename)=5;
--显示所有员工的姓名的第一个字
Select Ename,Substr(Ename,0,1) From Emp;
--假设一个月为30天,找出所有员工的日薪,不计小数
Select Ename,Sal,Round(Sal/30) Daysal From Emp;
--找到2月份受雇的员工
Select Ename,Hiredate From Emp Where To_Char(Hiredate,'fmmm')='2';
--找出姓名中不带R这个字母的员工(注意区分大小写)
Select Ename From Emp Where Ename Not Like '%R%';
--查找83年之后入职的员工。
Select Ename,Hiredate From Emp Where Hiredate>To_Date('19830101','yyyymmdd');
--Select Hiredate From emp;
--Lpad在列的左边填充字符
Select Empno, Lpad(Initcap(Trim(Ename)),10,'*') Name, Job, Sal From Emp;
附加上一些其他的再
Select Next_Day(Sysdate,'星期一') From Dual;
select last_day(sysdate) from dual;
Select To_Char(Sysdate,'yyyy') From Dual;
Select To_Char(Sysdate,'fmyyyy-mm-dd') From Dual;
Select To_Char(Sal,'L999,999,999') From Scott.Emp;
select to_char(sysdate,'D') from dual;--//返回星期
Select Empno, Ename, Sal, Comm, Coalesce(Sal+Comm, Sal, 0)总收入 From Emp;
--case语句
select empno, ename, sal,
case deptno
when 10 then '财务部'
when 20 then '研发部'
when 30 then '销售部'
else '未知部门'
End 部门
From Emp;