Ø 使用oracle的date类型时,最好使用24小时制。date类型精确的秒。 Ø oracle的日期在24小时制中00:00:00 在Oracle中表示为叫做上午12点,也就是零点。我们中午12点叫做下午12点。 Ø 世界是这样循环的:上午12点 -上午12点01 -上午1点 --上午11:59 :59 --下午12点 - 下午12点 02 - 下午1点 -下午11:59:59 Ø 关于日期函数 add_months (d,n)和last_day(d),round(d,【fmt】) u add_months (d,n) add_months 中的d 【1】如果没有时间值,结果为下个月的当前日的开始日期 -00:00:00上午12点。 【2】如果有时间值,结果为下月的当前日的时间加时间值。 例如: SQL> select sysdate,add_months(sysdate,1) from dual;
SYSDATE ADD_MONTHS(SYSDATE,1) ----------- --------------------- 2013/6/4 13 2013/7/4 13:49:32 SQL> select sysdate,add_months(sysdate,-2) from dual;
SYSDATE ADD_MONTHS(SYSDATE,-2) ----------- ---------------------- 2013/6/4 13 2013/4/4 13:50:37 u last_day 例如: SQL> select sysdate,last_day(sysdate) from dual;
SYSDATE LAST_DAY(SYSDATE) ----------- ----------------- 2013/6/4 13 2013/6/30 13:53:2 u round()与trunc() 例如: SQL> select to_date('2013-6-16','YYYY-MM-DD') d,round(to_date('2013-6-16','YYYY-MM-DD'),'year') year,round(to_date('2013-6-16','YYYY-MM-DD'),'month') month,round(to_date('2013-6-16','YYYY-MM-DD'),'day') day from dual;
D YEAR MONTH DAY ----------- ----------- ----------- ----------- 2013/6/16 2013/1/1 2013/7/1 2013/6/16 SQL> select sysdate,round(sysdate,'day') from dual;
SYSDATE ROUND(SYSDATE,'DAY') ----------- -------------------- 2013/6/4 14 2013/6/2 SQL> select sysdate,trunc(sysdate,'year') year,trunc(sysdate,'month') month,trunc(sysdate,'day') day from dual;
SYSDATE YEAR MONTH DAY ----------- ----------- ----------- ----------- 2013/6/4 13 2013/1/1 2013/6/1 2013/6/2 u extract() extract (fmt from d) 例如: SQL> select sysdate,extract(year from sysdate) y,extract(month from sysdate) m,extract(day from sysdate) d from dual;
SYSDATE Y M D ----------- ---------- ---------- ---------- 2013/6/4 17 2013 6 4 总结:oracle的日期函数默认操作日期,既不包括时间。如果有时间的话就加上时间,但不做处理。默认都是day。 Oracle关于时间/日期的操作 SQL> select to_char(sysdate,'YYYY/MM/DD HH24:MI:SS'),sysdate-interval '7' minute from dual;
TO_CHAR(SYSDATE,'YYYY/MM/DDHH2 SYSDATE-INTERVAL'7'MINUTE ------------------------------ ------------------------- 2013/06/04 17:19:27 2013/6/4 17:12:27 SQL> select to_char(sysdate,'YYYY/MM/DD HH24:MI:SS'),sysdate-interval '2' hour from dual;
TO_CHAR(SYSDATE,'YYYY/MM/DDHH2 SYSDATE-INTERVAL'2'HOUR ------------------------------ ----------------------- 2013/06/04 17:20:09 2013/6/4 15:20:09 SQL> select to_char(sysdate,'YYYY/MM/DD HH24:MI:SS'),sysdate-interval '2' day from dual;
TO_CHAR(SYSDATE,'YYYY/MM/DDHH2 SYSDATE-INTERVAL'2'DAY ------------------------------ ---------------------- 2013/06/04 17:21:33 2013/6/2 17:21:33 SQL> select to_char(sysdate,'YYYY/MM/DD HH24:MI:SS'),sysdate-interval '2' month from dual;
TO_CHAR(SYSDATE,'YYYY/MM/DDHH2 SYSDATE-INTERVAL'2'MONTH ------------------------------ ------------------------ 2013/06/04 17:22:23 2013/4/4 17:22:23 SQL> select to_char(sysdate,'YYYY/MM/DD HH24:MI:SS'),sysdate-interval '2' year from dual;
TO_CHAR(SYSDATE,'YYYY/MM/DDHH2 SYSDATE-INTERVAL'2'YEAR ------------------------------ ----------------------- 2013/06/04 17:22:50 2011/6/4 17:22:50 SQL> select to_char(sysdate,'YYYY/MM/DD HH24:MI:SS'),sysdate - 8 *interval '2' hour from dual;
SQL> select to_char(sysdate,'YYYY/MM/DD HH24:MI:SS'),sysdate - 2 *interval '2' hour from dual;
TO_CHAR(SYSDATE,'YYYY/MM/DDHH2 SYSDATE-2*INTERVAL'2'HOUR ------------------------------ ------------------------- 2013/06/04 17:26:19 2013/6/4 13:26:19 SQL> select sysdate,trunc(sysdate) from dual;
SYSDATE TRUNC(SYSDATE) ----------- -------------- 2013/6/4 17 2013/6/4 SQL> select to_char(current_timestamp(2),'DD-MON-YYYY HH24:MI:SSxFF') a, to_char(current_timestamp(5),'DD-MON-YYYY HH24:MI:SSxFF') b from dual;
A B ----------------------------------- ------------------ 04-6月 -2013 17:31:37.26 04-6月 -2013 17:31:37.25700 SQL> select to_char(current_timestamp(9),'MI:SSxFF') from dual;
TO_CHAR(CURRENT_TIMESTAMP(9),' ------------------------------ 30:31.221000000 7.取得当前日期是本月的第几周 SQL> select sysdate,to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual;
SYSDATE TO_CHAR(SYSDATE,'YYYYMMDDWHH24 ----------- ------------------------------ 2013/6/4 17 20130604 1 17:34:13 SQL> select sysdate,to_char(sysdate,'W') from dual;
SYSDATE TO_CHAR(SYSDATE,'W') ----------- -------------------- 2013/6/4 17 1 8.取得当前日期是一个星期中的第几天,注意星期日是第一天 SQL> select sysdate,to_char(sysdate,'D') from dual;
SYSDATE TO_CHAR(SYSDATE,'D') ----------- -------------------- 2013/6/4 17 3 类似: select to_char(sysdate,'yyyy') from dual; --年 SQL> select sysdate,to_char(sysdate,'yyyy') from dual;
SYSDATE TO_CHAR(SYSDATE,'YYYY') ----------- ----------------------- 2013/6/4 17 2013 SQL> select sysdate,to_char(sysdate,'Q') from dual;
SYSDATE TO_CHAR(SYSDATE,'Q') ----------- -------------------- 2013/6/4 17 2 SQL> select sysdate,to_char(sysdate,'mm') from dual;
SYSDATE TO_CHAR(SYSDATE,'MM') ----------- --------------------- 2013/6/4 17 06 SQL> select sysdate,to_char(sysdate,'dd') from dual;
SYSDATE TO_CHAR(SYSDATE,'DD') ----------- --------------------- 2013/6/4 17 04 ddd 年中的第几天 SQL> select sysdate,to_char(sysdate,'WW'),to_char(sysdate,'ddd') from dual;
SYSDATE TO_CHAR(SYSDATE,'WW') TO_CHAR(SYSDATE,'DDD') ----------- --------------------- ---------------------- 2013/6/4 17 23 155 9:取当前日期是星期几中文显示: SQL> select sysdate,to_char(sysdate,'day') from dual;
SYSDATE TO_CHAR(SYSDATE,'DAY') ----------- ---------------------- 2013/6/4 17 星期二 10.如果一个表在一个date类型的字段上面建立了索引,如何使用 alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
SYSDATE ----------- 2013/6/4 17 12.得到当天凌晨0点0分0秒的日期
SYSDATE TRUNC(SYSDATE) ----------- -------------- 2013/6/4 17 2013/6/4 13.得到这天的最后一秒
SYSDATE TRUNC(SYSDATE)+0.99999 ----------- ---------------------- 2013/6/4 17 2013/6/4 23:59:59 14.得到小时的具体数值 SQL> select sysdate,trunc(sysdate) + 1/24 from dual;
SYSDATE TRUNC(SYSDATE)+1/24 ----------- ------------------- 2013/6/4 17 2013/6/4 1:00:00 select trunc(sysdate) + 7/24 from dual; SQL> select sysdate,trunc(sysdate) + 7/24 from dual;
SYSDATE TRUNC(SYSDATE)+7/24 ----------- ------------------- 2013/6/4 17 2013/6/4 7:00:00 15.得到明天凌晨0点0分0秒的日期 select trunc(sysdate+1) from dual; SQL> select sysdate,trunc(sysdate+1) from dual;
SYSDATE TRUNC(SYSDATE+1) ----------- ---------------- 2013/6/4 17 2013/6/5 SQL> select sysdate,trunc(sysdate,'mm') from dual;
SYSDATE TRUNC(SYSDATE,'MM') ----------- ------------------- 2013/6/4 17 2013/6/1 SQL> select sysdate,trunc(add_months(sysdate,1),'mm') from dual;
SYSDATE TRUNC(ADD_MONTHS(SYSDATE,1),'M ----------- ------------------------------ 2013/6/4 17 2013/7/1 18.返回当前月的最后一天? SQL> select sysdate,last_day(sysdate) from dual;
SYSDATE LAST_DAY(SYSDATE) ----------- ----------------- 2013/6/8 9: 2013/6/30 9:55:09 SQL> select trunc(sysdate),last_day(trunc(sysdate)) from dual;
TRUNC(SYSDATE) LAST_DAY(TRUNC(SYSDATE)) -------------- ------------------------ 2013/6/8 2013/6/30 SQL> select trunc(sysdate),trunc(last_day(sysdate)) from dual;
TRUNC(SYSDATE) TRUNC(LAST_DAY(SYSDATE)) -------------- ------------------------ 2013/6/8 2013/6/30 SQL> select sysdate,add_months(sysdate,1) a,trunc(add_months(sysdate,1),'mm') b,trunc(add_months(sysdate,1),'mm')-1 c from dual;
SYSDATE A B C ----------- ----------- ----------- ----------- 2013/6/8 9: 2013/7/8 9: 2013/7/1 2013/6/30 from (select rownum rn from all_objects where rownum<366);
TO_CHAR(SYSDATE,'DDD') ---------------------- 159 21.如何在给现有的日期加上2年
SYSDATE ADD_MONTHS(SYSDATE,24) ----------- ---------------------- 2013/6/8 10 2015/6/8 10:05:45 22:判断某一日子所在年分是否为润年 SQL> select sysdate,decode(to_char(last_day(trunc(sysdate,'y')+31),'dd'),'29','闰年','平年') from dual;
SYSDATE DECODE(TO_CHAR(LAST_DAY(TRUNC( ----------- ------------------------------ 2013/6/8 10 平年 23:判断两年后是否为润年 SQL> select trunc(sysdate),decode(to_char(last_day(trunc(add_months(sysdate,24),'y')+31),'dd'),'29','闰年','平年') from dual;
TRUNC(SYSDATE) DECODE(TO_CHAR(LAST_DAY(TRUNC( -------------- ------------------------------ 2013/6/8 平年 select ceil(to_number(to_char(sysdate,'mm'))/3) from dual; SQL> select trunc(sysdate) a,ceil(to_number(to_char(sysdate,'mm'))/3) b from dual;
A B ----------- ---------- 2013/6/8 2 select to_char(sysdate, 'Q') from dual; SQL> select trunc(sysdate),to_char(sysdate, 'Q') from dual;
TRUNC(SYSDATE) TO_CHAR(SYSDATE,'Q') -------------- -------------------- 2013/6/8 2 |