ORACLE的常用99条语句

263 阅读8分钟

ORACLE的常用99条语句

  1. select * from emp;

  2. select empno, ename, job from emp;

  3. select empno 编号, ename 姓名, job 工作 from emp;

  4. select job from emp;

  5. select distinct job from emp;

  6. select distinct empno, job from emp; 说明:因为雇员编号不重复, 所以此时证明所有的列没有重复,所以不能消除掉重复的列.

  7. 查询出雇员的编号, 姓名, 工作, 但是显示的格式:编号是: 7369 的雇员, 姓名是: smith, 工作是: clear select '编号是: ' || empno || '的雇员, 姓名是: ' || ename || ', 工作是: ' || job from emp;

  8. 求出每个雇员的姓名及年薪 select ename, sal * 12 income from emp;

  9. 求出工资大于 1500 的所有雇员信息 select * from emp where sal > 1500;

  10. 查询每月可以得到奖金的雇员信息 select * from emp where comm is not null;

  11. 查询没有奖金的雇员信息 select * from emp where comm is null;

  12. 查询出基本工资大于 1500 同时可以领取奖金的雇员信息 select * from emp where sal > 1500 and comm is not null;

  13. 查询出基本工资大于 1500 或者可以领取奖金的雇员信息 select * from emp where sal > 1500 or comm is not null;

  14. 查询出基本工资不大于 1500 或者不可以领取奖金的雇员信息 select * from emp where not(sal > 1500 and comm is not null);

  15. 查询基本工资大于 1500, 但是小于 3000 的全部雇员信息 select * from emp where sal > 1500 and sal < 3000;

  16. 查询基本工资大于等于 1500, 但是小于等于 3000 的全部雇员信息 select * from emp where sal >= 1500 and sal <= 3000; select * from emp where sal between 1500 and 3000;

  17. 查询出在 1981 年雇佣的全部雇员信息(1981 年 1 月 1 日 到 1981 年 12 月 31 日之间的雇佣的雇员) select * from emp where hiredate between '1-1月-81' and '31-12月-81';

  18. 要求查询出姓名是 smith 的雇员信息 select * from emp where ename = 'SMITH';

  19. 要求查询出雇员是 7369, 7499, 7521 的雇员的具体信息 select * from emp where empno = 7369 or empno = 7499 or empno = 7521; select * from emp where empno in(7369, 7499, 7521);

  20. 要求查询出雇员不是 7369, 7499, 7521 的雇员的具体信息 select * from emp where empno not in(7369, 7499, 7521);

  21. 要求查询出姓名是 smith, allen, king 的雇员信息 select * from emp where ename in('SMITH', 'ALLEN', 'KING');

  22. 查询出所有雇员姓名中第二个字母包含 "M" 的雇员信息 select * from emp where ename like '_M%';

  23. 查询出雇员姓名中包含字母 M 的雇员信息 select * from emp where ename like '%M%';

  24. 要求查询出在 1981 年雇佣的雇员信息 select * from emp where hiredate like '%81%';

  25. 查询工资中包含 5 的雇员信息 select * from emp where sal like '%5%';

  26. 查询雇员编号不是 7369 的雇员信息 select * from emp where empno != 7369; select * from emp where empno <> 7369;

  27. 要求按照工资由低到高排序 select * frm emp order by sal; select * from emp order by sal asc;

  28. 要求按照工资由高到低排序 select * from emp order by sal desc;

  29. 要求查询出 20 部门的所有雇员信息, 查询的信息按照工资由高到低排序,如果工资相等,则按照雇佣日期由早到晚排序. select * from emp where deptno = 20 order by sal desc, hiredate asc;

  30. 将小写字母变为大写字母 select upper('hello') from dual;

  31. 将大写字母变为小写字母 select lower('HELLO WORLD') from dual;

  32. 要求查询出姓名是 smith 的雇员信息 select * from emp where ename = upper('smith');

  33. 使用 initcap() 函数将单词的第一个字母大写 select initcap('hello world') from dual;

  34. 将雇员表中的雇员姓名变为开头字母大写 select initcap(ename) from emp;

  35. 将字符串 "hello" 和 "world" 进行串联 select concat('hello ', 'world') from dual;

  36. 对字符串进行操作的常用字符处理函数 select substr('hello', 1, 3) 截取字符串, length('hello') 字符串的长度, replace('hello', 'l', 'x') 字符串替换 from dual; select substr('hello', 0, 3) 截取字符串, length('hello') 字符串的长度, replace('hello', 'l', 'x') 字符串替换 from dual;

  37. 显示所有雇员的姓名及姓名的后三个字符 select ename, substr(ename, length(ename) -2) from emp; select ename, substr(ename, -3, 3) from emp;

  38. 使用数值函数执行四舍五入操作 select round(789.536) from dual;

  39. 要求将 789.536 数值保留两位小数 select round(789.536, 2) from dual;

  40. 要求将 789.536 数值中的整数的十位进行四舍五入进位 select round(789.536, -2) from dual;

  41. 采用 trunc() 函数不会保留任何小数,而且小数点也不会执行四舍五入的操作 select trunc(789.536) from dual;

  42. 通过 trunc() 也可以指定小数点的保留位数 select trunc(789.536, 2) from dual;

  43. 作用负数表示位数 select trunc(789.536, -2) from dual;

  44. 使用 mod() 函数可以进行取余的操作 select mod(10, 3) from dual;

  45. 显示 10 部门雇员进入公司的星期数(当前日期 - 雇佣日期 = 天数 / 7 = 星期数) select empno, ename, round((sysdate - hiredate) / 7) from emp where deptno = 10;

  46. 日期函数 months_between(): 求出给定日期范围的月数 add_months(): 在指定的日期上加上指定的月数, 求出之后的日期 next_day(): 指定日期的下一个日期 last_day(): 求出给定日期当月的最后一天日期

  47. select empno, ename, months_between(sysdate, hiredate) from emp; select empno, ename, round(months_between(sysdate, hiredate)) from emp;

  48. select sysdate, add_months(sysdate, 4) from dual;

  49. select next_day(sysdate, '星期一') from dual;

  50. select last_day(sysdate) from dual;

  51. 转换函数 to_char(): 转换成字符串 to_number(): 转换成数字 to_date(): 转换成日期

  52. 查询所有雇员的雇员编号, 姓名, 雇佣日期 select empno, ename, to_char(hiredate, 'yyyy') year, to_char(hiredate, 'mm') months, to_char(hiredate, 'dd') day from emp;

select empno, ename, to_char(hiredate, 'yyyy-mm-dd') from emp;

select empno, ename, to_char(hiredate, 'fmyyyy-mm-dd') from emp;

  1. 查询所有雇员的编号, 姓名和工资 select empno, ename, sal from emp; select empno, ename, to_char(sal, '99,999') from emp; select empno, ename, to_char(sal, 'L99,999') from emp; select empno, ename, to_char(sal, '$99,999') from emp;

  2. select to_number('123') + to_number('123') from dual;

  3. 将一个字符串转换成日期类型 select to_date('2009-01-01', 'yyyy-mm-dd') from dual;

  4. 求出每个雇员的年薪(要求加上奖金) select empno, ename, sal, comm, (sal + comm) * 12 from emp; select empno, ename, sal, comm, nvl(comm, 0), (sal + nvl(comm, 0)) * 12 income from emp;

  5. decode() 函数类似于 if....elsif...else 语句 select decode(1, 1, '内容是 1', 2, '内容是 2', 3, '内容是 3') from dual;

  6. 查询出雇员的编号, 姓名, 雇佣日期及工作, 要求将雇员的工作替换成以下信息: select empno 雇员编号, ename 雇员姓名, hiredate 雇佣日期, decode(job, 'CLERK', '业务员', 'SALESMAN', '销售人员', 'MANAGER', '经理', 'ANALYST', '分析员', 'PRESIDENT', '总裁' ) 职位 from emp;

  7. 笛卡尔积(交差连接) select * from emp, dept; select * from emp cross join dept;

  8. 内连接 select * from emp e, dept d where e.deptno = d.deptno; select * from emp e inner join dept d on e.deptno = d.deptno; select * from emp e join dept d on e.deptno = d.deptno;

  9. 自然连接 select * from emp natural join dept; select * from emp e join dept d using(deptno);

  10. 要求查询出雇员的编号, 姓名, 部门的编号, 名称, 地址 select e.empno, e.ename, d.deptno, d.dname, d.loc from emp e, dept d where e.deptno = d.deptno;

  11. 要求查询出雇员的姓名, 工作, 雇员的直接上级领导姓名 select e.ename, e.job, m.ename from emp e, emp m where e.mgr = m.empno;

  12. 要求查询出雇员的姓名, 工作, 雇员的直接上级领导姓名以及部门名称 select e.ename, e.job, m.ename, d.dname from emp e, emp m, dept d where e.mgr = m.empno and e.deptno = d.deptno;

  13. 要求查询出每个雇员的姓名, 工资, 部门名称, 工资在公司的等级(salgrade), 及其领导的姓名及工资所在公司的等级 select e.ename, e.sal, d.dname, s.grade, m.ename, m.sal, ms.grade from emp e, dept d, salgrade s, emp m, salgrade ms where e.deptno = d.deptno and e.sal between s.losal and s.hisal and e.mgr = m.empno and m.sal between ms.losal and ms.hisal;

select e.ename, e.sal, d.dname, decode(s.grade, 1, '第五等级', 2, '第四等级', 3, '第三等级', 4, '第二等级', 5, '第一等级'), m.ename, m.sal, decode(ms.grade, 1, '第五等级', 2, '第四等级', 3, '第三等级', 4, '第二等级', 5, '第一等级') from emp e, dept d, salgrade s, emp m, salgrade ms where e.deptno = d.deptno and e.sal between s.losal and s.hisal and e.mgr = m.empno and m.sal between ms.losal and ms.hisal;

  1. select empno, ename, d.deptno, dname, loc from emp e, dept d where e.deptno = d.deptno; select empno, ename, d.deptno, dname, loc from emp e inner join dept d on e.deptno = d.deptno;

  2. 左外连接 select empno, ename, d.deptno, dname, loc from emp e, dept d where e.deptno = d.deptno(+); select empno, ename, d.deptno, dname, loc from emp e left outer join dept d on e.deptno = d.deptno; select empno, ename, d.deptno, dname, loc from emp e left join dept d on e.deptno = d.deptno(+);

  3. 右外连接 select empno, ename, d.deptno, dname, loc from emp e, dept d where e.deptno(+) = d.deptno; select empno, ename, d.deptno, dname, loc from emp e right outer join dept d on e.deptno = d.deptno; select empno, ename, d.deptno, dname, loc from emp e right join dept d on e.deptno = d.deptno;

  4. select e.empno, e.ename, m.empno, m.ename from emp e, emp m where e.mgr = m.empno;

  5. select e.empno, e.ename, m.empno, m.ename from emp e, emp m where e.mgr = m.empno(+);

  6. select * from emp e, dept d where e.deptno = d.deptno and d.deptno = 30; select * from emp e inner join dept d on e.deptno = d.deptno where d.deptno = 30; select * from emp e join dept d on e.deptno = d.deptno where d.deptno = 30; select * from emp e natural join dept d where deptno = 30; select * from emp e join dept d using(deptno) where deptno = 30;

  7. select e.ename, d.deptno, d.dname, d.loc from emp e right outer join dept d on e.deptno = d.deptno; select e.ename, d.deptno, d.dname, d.loc from emp e right join dept d on e.deptno = d.deptno; select e.ename, d.deptno, d.dname, d.loc from emp e, dept d where e.deptno(+) = d.deptno;

  8. select count(ename) from emp;

  9. select min(sal) from emp;

  10. select max(sal) from emp;

  11. select sum(sal) from emp;

  12. select avg(sal) from emp;

  13. select sum(sal) from emp where deptno = 20;

  14. select avg(sal) from emp where deptno = 20;

  15. 求出每个部门的雇员数量 select deptno, count(deptno) from emp group by deptno; select deptno, count(empno) from emp group by deptno;

  16. 求出每个部门的平均工资 select deptno, avg(sal) from emp group by deptno;

  17. 按部门分组, 并显示部门的名称, 及每个部门的员工数 select d.dname, count(e.empno) from emp e, dept d where e.deptno = d.deptno group by d.dname;

select d.deptno, d.dname, temp.c from (select deptno, count(e.empno) c from emp e group by e.deptno) temp, dept d where temp.deptno = d.deptno;

  1. 要求显示出平均工资大于 2000 的部门编号和平均工资 select deptno, avg(sal) from emp group by deptno having avg(sal) > 2000;

  2. 显示非销售人员工作名称以及从事同一工作雇员的月工资的总和,并且要满足从事同一工作的雇员的月工资合计大于 5000, 输出结果按月工资的合计升序排序. select job, sum(sal) su from emp where job <> 'SALESMAN' group by job having sum(sal) > 5000 order by su;

select temp.job, sum(temp.sal) s from (select job, sal from emp e where job <> 'SALESMAN') temp group by temp.job having sum(temp.sal) > 5000 order by s;

  1. 求出平均工资最高的部门工资 select max(avg(sal)) from emp group by deptno;

  2. 要求查询出比雇员编号为 7654 工资高的所有雇员信息 select * from emp where sal >(select sal from emp where empno = 7654);

  3. 要求查询出工资比 7654 高, 同时与 7788 从事相同工作的全部雇员信息 select * from emp where sal >(select sal from emp where empno = 7654) and job = (select job from emp where empno = 7788);

  4. 要求查询出工资最低的雇员姓名, 工作, 工资 select ename, job, sal from emp where sal = (select min(sal) from emp);

  5. 要求查询出: 部门名称,部门的员工数,部门的平均工资,部门的最低收入雇员的姓名 select d.dname, temp.c, temp.a, e.ename from dept d, (select deptno, count(empno) c, avg(sal) a, min(sal) m from emp group by deptno) temp, emp e where d.deptno = temp.deptno and e.sal = temp.m;

select d.deptno, temp.dname, temp.c, temp.a, e.ename, e.sal from (select d.dname , count(e.empno) c, avg(e.sal) a, min(e.sal) m from emp e, dept d where e.deptno = d.deptno group by d.dname) temp, emp e, dept d where temp.m = e.sal and temp.dname = d.dname;

  1. 求出每个部门的最低工资的雇员的信息 select * from emp where sal in(select min(sal) from emp group by deptno); select * from emp where sal =any(select min(sal) from emp group by deptno); select * from (select min(sal) m from emp group by deptno) temp, emp e where e.sal = temp.m;

  2. 范例 90 中, 比子查询条件中最低(小)的工资要大的雇员信息 select * from emp where sal >any(select min(sal) from emp group by deptno); select * from emp where sal > (select min(min(sal)) from emp group by deptno);

  3. 范例 90 中, 比子查询条件中最高(大)的工资要小的雇员信息 select * from emp where sal <any(select min(sal) from emp group by deptno); select * from emp where sal < (select max(min(sal)) from emp group by deptno);

93. 范例 90 中, 比子查询条件中最高(大)的工资要大的雇员信息 select * from emp where sal >all(select min(sal) from emp group by deptno); select * from emp where sal > (select max(min(sal)) from emp group by deptno);

  1. 范例 90 中, 比子查询条件中最低(小)的工资要小的雇员信息 select * from emp where sal <all(select min(sal) from emp group by deptno); select * from emp where sal < (select min(min(sal)) from emp group by deptno);

  2. 查找出 20 部门中没有奖金的雇员信息 select * from emp where (sal, nvl(comm, -1)) in (select sal, nvl(comm, -1) from emp where deptno = 20); select * from emp where deptno = 20 and comm is null;

  3. union 操作符返回两个查询选定的所有不重复的行 select deptno from emp union select deptno from dept;

  4. union all 操作符合并两个查询选定的所有行,包括重复的行 select deptno from emp union all select deptno from dept;

  5. intersect 操作符只返回两个查询都有的行 select deptno from emp intersect select deptno from dept;

  6. minus 操作符只返回由第一个查询选定但是没有被第二个查询选定的行, 也就是在第一个查询结果中排除在第二个查询结果中出现的行 select deptno from dept minus select deptno from emp;

ORACLE中的日期函数的使用

TO_DATE格式(以时间:2007-11-02 13:45:25为例)

    Year:      
    yy two digits 两位年                显示值:07
    yyy three digits 三位年                显示值:007
    yyyy four digits 四位年                显示值:2007

    Month:      
    mm    number     两位月              显示值:11
    mon    abbreviated 字符集表示          显示值:11月,若是英文版,显示nov     
    month spelled out 字符集表示          显示值:11月,若是英文版,显示november 

    Day:      
    dd    number         当月第几天        显示值:02
    ddd    number         当年第几天        显示值:02
    dy    abbreviated 当周第几天简写    显示值:星期五,若是英文版,显示fri
    day    spelled out   当周第几天全写    显示值:星期五,若是英文版,显示friday        
    ddspth spelled out, ordinal twelfth 

          Hour:
          hh    two digits 12小时进制            显示值:01
          hh24 two digits 24小时进制            显示值:13

          Minute:
          mi    two digits 60进制                显示值:45

          Second:
          ss    two digits 60进制                显示值:25

          其它
          Q     digit         季度                  显示值:4
          WW    digit         当年第几周            显示值:44
          W    digit          当月第几周            显示值:1

    24小时格式下时间范围为: 0:00:00 - 23:59:59....      
    12小时格式下时间范围为: 1:00:00 - 12:59:59 .... 
  1. 日期和字符转换函数用法(to_date,to_char)

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; //日期转化为字符串
select to_char(sysdate,'yyyy') as nowYear from dual; //获取时间的年
select to_char(sysdate,'mm') as nowMonth from dual; //获取时间的月
select to_char(sysdate,'dd') as nowDay from dual; //获取时间的日
select to_char(sysdate,'hh24') as nowHour from dual; //获取时间的时
select to_char(sysdate,'mi') as nowMinute from dual; //获取时间的分
select to_char(sysdate,'ss') as nowSecond from dual; //获取时间的秒

select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss') from dual//

  1. select to_char( to_date(222,'J'),'Jsp') from dual

    显示Two Hundred Twenty-Two

3.求某天是星期几
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;
星期一
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;
monday
设置日期语言
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
也可以这样
TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')

  1. 两个日期间的天数
    select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;

  2. 时间为null的用法
    select id, active_date from table1
    UNION
    select 1, TO_DATE(null) from dual;
    注意要用TO_DATE(null)

6.月份差
a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')
那么12月31号中午12点之后和12月1号的12点之前是不包含在这个范围之内的。
所以,当时间需要精确的时候,觉得to_char还是必要的

  1. 日期格式冲突问题
    输入的格式要看你安装的Oracle字符集的类型, 比如: US7ASCII, date格式的类型就是: '01-Jan-01'
    alter system set NLS_DATE_LANGUAGE = American
    alter session set NLS_DATE_LANGUAGE = American
    或者在to_date中写
    select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;
    注意我这只是举了NLS_DATE_LANGUAGE,当然还有很多,
    可查看
    select * from nls_session_parameters
    select * from V$NLS_PARAMETERS

复制代码 代码如下:

select count(*)
from ( select rownum-1 rnum
from all_objects
where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-
02-01','yyyy-mm-dd')+1
)
where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )
not in ( '1', '7' )

查找2002-02-28至2002-02-01间除星期一和七的天数
在前后分别调用DBMS_UTILITY.GET_TIME, 让后将结果相减(得到的是1/100秒, 而不是毫秒).

  1. 查找月份
    复制代码 代码如下:

    select months_between(to_date('01-31-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
    1
    select months_between(to_date('02-01-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
    1.03225806451613

  2. Next_day的用法
    复制代码 代码如下:

    Next_day(date, day)

    Monday-Sunday, for format code DAY
    Mon-Sun, for format code DY
    1-7, for format code D

11
select to_char(sysdate,'hh:mi:ss') TIME from all_objects
注意:第一条记录的TIME 与最后一行是一样的
可以建立一个函数来处理这个问题
复制代码 代码如下:

create or replace function sys_date return date is
begin
return sysdate;
end;

select to_char(sys_date,'hh:mi:ss') from all_objects;

12.获得小时数
extract()找出日期或间隔值的字段值 复制代码 代码如下:

SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer      
SQL> select sysdate ,to_char(sysdate,'hh') from dual;      

SYSDATE TO_CHAR(SYSDATE,'HH')      
-------------------- ---------------------      
2003-10-13 19:35:21 07      

SQL> select sysdate ,to_char(sysdate,'hh24') from dual;      

SYSDATE TO_CHAR(SYSDATE,'HH24')      
-------------------- -----------------------      
2003-10-13 19:35:21 19     

13.年月日的处理
复制代码 代码如下:

select older_date,
newer_date,
years,
months,
abs(
trunc(
newer_date-
add_months( older_date,years*12+months )
)
) days

from ( select
trunc(months_between( newer_date, older_date )/12) YEARS,
mod(trunc(months_between( newer_date, older_date )),12 ) MONTHS,
newer_date,
older_date
from ( select hiredate older_date, add_months(hiredate,rownum)+rownum newer_date
from emp )
)

14.处理月份天数不定的办法
select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual
16.找出今年的天数
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual
闰年的处理方法
to_char( last_day( to_date('02' | | :year,'mmyyyy') ), 'dd' )
如果是28就不是闰年
17.yyyy与rrrr的区别
复制代码 代码如下:

'YYYY99 TO_C


yyyy 99 0099
rrrr 99 1999
yyyy 01 0001
rrrr 01 2001

18.不同时区的处理
select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdate
from dual;

19.5秒钟一个间隔
复制代码 代码如下:

Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,TO_CHAR(sysdate,'SSSSS')
from dual
2002-11-1 9:55:00 35786
SSSSS表示5位秒数

20.一年的第几天
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual

310 2002-11-6 10:03:51

21.计算小时,分,秒,毫秒 复制代码 代码如下:

 select      
 Days,      
 A,      
 TRUNC(A*24) Hours,      
 TRUNC(A*24*60 - 60*TRUNC(A*24)) Minutes,      
 TRUNC(A*24*60*60 - 60*TRUNC(A*24*60)) Seconds,      
 TRUNC(A*24*60*60*100 - 100*TRUNC(A*24*60*60)) mSeconds      
from      
(      
 select      
 trunc(sysdate) Days,      
 sysdate - trunc(sysdate) A      
 from dual      

)
select * from tabname
order by decode(mode,'FIFO',1,-1)*to_char(rq,'yyyymmddhh24miss');

//
floor((date2-date1) /365) 作为年
floor((date2-date1, 365) /30) 作为月
d(mod(date2-date1, 365), 30)作为日.

23.next_day函数 返回下个星期的日期,day为1-7或星期日-星期六,1表示星期日 next_day(sysdate,6)是从当前开始下一个星期五。后面的数字是从星期日开始算起。
1 2 3 4 5 6 7
日 一 二 三 四 五 六


select (sysdate-to_date('2003-12-03 12:55:45','yyyy-mm-dd hh24:mi:ss'))2460*60 from ddual 日期 返回的是天 然后 转换为ss

24,round舍入到最接近的日期 select sysdate S1, round(sysdate) S2 , round(sysdate,'year') YEAR, round(sysdate,'month') MONTH , round(sysdate,'day') DAY from dual

25,trunc[截断到最接近的日期,单位为天] ,返回的是日期类型 select sysdate S1,
trunc(sysdate) S2, //返回当前日期,无时分秒 trunc(sysdate,'year') YEAR, //返回当前年的1月1日,无时分秒 trunc(sysdate,'month') MONTH , //返回当前月的1日,无时分秒 trunc(sysdate,'day') DAY //返回当前星期的星期天,无时分秒 from dual

26,返回日期列表中最晚日期 select greatest('01-1月-04','04-1月-04','10-2月-04') from dual

27.计算时间差 注:oracle时间差是以天数为单位,所以换算成年月,日

  select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from dual        //时间差-年
  select ceil(moths_between(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanMonths from dual        //时间差-月
  select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanDays from dual             //时间差-天
  select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from dual         //时间差-时
  select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60) as spanMinutes from dual    //时间差-分
  select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60*60) as spanSeconds from dual //时间差-秒

28.更新时间 注:oracle时间加减是以天数为单位,设改变量为n,所以换算成年月,日 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n*365,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-年 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),add_months(sysdate,n) as newTime from dual //改变时间-月 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-日 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n/24,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-时 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n/24/60,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-分 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n/24/60/60,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-秒

29.查找月的第一天,最后一天 SELECT Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month, Trunc(SYSDATE, 'MONTH') - 1 / 86400 Last_Day_Last_Month, Trunc(SYSDATE, 'MONTH') First_Day_Cur_Month, LAST_DAY(Trunc(SYSDATE, 'MONTH')) + 1 - 1 / 86400 Last_Day_Cur_Month FROM dual;