【SQL】時間遞增的兩種方法

secooler發表於2010-05-14
經常被問到:如何對現有的日期欄位增加一秒鐘、增加一分鐘、增加一小時、增加一天等。
這裡給出兩種方法,供參考。

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 --

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-662804/,如需轉載,請註明出處,否則將追究法律責任。

相關文章