【SQL】时间递增的两种方法

经常被问到:如何对现有的日期字段增加一秒钟、增加一分钟、增加一小时、增加一天等。
这里给出两种方法,供参考。

1.增加一秒钟
sec@ora10g> select to_date('20100514000000','yyyymmddhh24miss'),to_date('20100514000000','yyyymmddhh24miss')+1/24/60/60 from dual;

TO_DATE('2010051400 TO_DATE('2010051400
------------------- -------------------
2010-05-14 00:00:00 2010-05-14 00:00:01

sec@ora10g> select to_date('20100514000000','yyyymmddhh24miss'),to_date('20100514000000','yyyymmddhh24miss')+ interval '1' second from dual;

TO_DATE('2010051400 TO_DATE('2010051400
------------------- -------------------
2010-05-14 00:00:00 2010-05-14 00:00:01

2.增加一分钟
sec@ora10g> select to_date('20100514000000','yyyymmddhh24miss'),to_date('20100514000000','yyyymmddhh24miss')+1/24/60 from dual;

TO_DATE('2010051400 TO_DATE('2010051400
------------------- -------------------
2010-05-14 00:00:00 2010-05-14 00:01:00

sec@ora10g> select to_date('20100514000000','yyyymmddhh24miss'),to_date('20100514000000','yyyymmddhh24miss')+ interval '1' minute from dual;

TO_DATE('2010051400 TO_DATE('2010051400
------------------- -------------------
2010-05-14 00:00:00 2010-05-14 00:01:00

3.增加一小时
sec@ora10g> select to_date('20100514000000','yyyymmddhh24miss'),to_date('20100514000000','yyyymmddhh24miss')+1/24 from dual;

TO_DATE('2010051400 TO_DATE('2010051400
------------------- -------------------
2010-05-14 00:00:00 2010-05-14 01:00:00

sec@ora10g> select to_date('20100514000000','yyyymmddhh24miss'),to_date('20100514000000','yyyymmddhh24miss')+ interval '1' hour from dual;

TO_DATE('2010051400 TO_DATE('2010051400
------------------- -------------------
2010-05-14 00:00:00 2010-05-14 01:00:00

4.增加一天
sec@ora10g> select to_date('20100514000000','yyyymmddhh24miss'),to_date('20100514000000','yyyymmddhh24miss')+1 from dual;

TO_DATE('2010051400 TO_DATE('2010051400
------------------- -------------------
2010-05-14 00:00:00 2010-05-15 00:00:00

sec@ora10g> select to_date('20100514000000','yyyymmddhh24miss'),to_date('20100514000000','yyyymmddhh24miss')+ interval '1' day from dual;

TO_DATE('2010051400 TO_DATE('2010051400
------------------- -------------------
2010-05-14 00:00:00 2010-05-15 00:00:00

5.小结
只要掌握了日期类型的递增规律一切都是那样的自然与和谐。

Good luck.

secooler
10.05.14

-- The End --

请使用浏览器的分享功能分享到微信等