Add 2 months to current timestamp

无人久伴 提交于 2019-11-27 08:09:36

问题


How can I add months to a timestamp value in Oracle? In my query, it's getting converted to date value instead:

     SELECT add_months(current_timestamp,2) 
     FROM   dual;

The actual output is:

     ADD_MONTH
     11-MAR-13

The expected output is:

    2013-01-01 00:00:00.000000000+00:00

回答1:


This will give you the date and the time as a TIMESTAMP data type:

select TO_TIMESTAMP(TO_CHAR(ADD_MONTHS(SYSDATE, 2), 'YYYYMMDD HH24:MI'), 
'YYYYMMDD HH24:MI') from dual;

If you need more or less precision (E.G. rounding) than what is above, adjust the date formats (both need to be the same format). For example, this will return 2 months down to the seconds level of precision:

select TO_TIMESTAMP(TO_CHAR(ADD_MONTHS(SYSTIMESTAMP, 2), 
'YYYYMMDD HH24:MI:SS'), 'YYYYMMDD HH24:MI:SS') from dual;

This is the closest I can get (as a character) to the format you need:

select TO_CHAR( 
TO_TIMESTAMP(TO_CHAR(ADD_MONTHS(SYSTIMESTAMP, 2), 
'YYYYMMDD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS'),
'YYYY-MM-DD HH24:MI:SS.FF TZR') from dual;



回答2:


I think this will about give you what you're looking for:

SELECT TO_CHAR(TO_TIMESTAMP(ADD_MONTHS(CURRENT_TIMESTAMP,2))
            + (CURRENT_TIMESTAMP - TRUNC(CURRENT_TIMESTAMP)),
       'YYYY-MM-DD HH:MI:SSxFFTZR') FROM DUAL;

The problem with using the interval methods is that you can get an unexpected error depending on the date you run the query. E.g.

SELECT TO_TIMESTAMP('31-JAN-2012') + NUMTOYMINTERVAL(1,'MONTH') FROM DUAL;

That query returns:

ORA-01839: date not valid for month specified

This is because it attempts to return February 31, which is not a valid date.

ADD_MONTHS is a "safer" way to date math, in that where the interval query would throw an error, ADD_MONTHS will return the last date of the month (Feb 28 or 29 depending on the year) in the above example.




回答3:


For Oracle:

SELECT
  TIMESTAMP'2014-01-30 08:16:32',                                  -- TS we want to increase by 1 month
--TIMESTAMP'2014-01-30 08:16:32' + NUMTOYMINTERVAL(1, 'MONTH'),    -- raises ORA-01839: date not valid for month specified
--TIMESTAMP'2014-01-30 08:16:32' + INTERVAL '1' MONTH,             -- raises ORA-01839: date not valid for month specified
  ADD_MONTHS(TIMESTAMP'2014-01-30 08:16:32', 1),                   -- works but is a date :(
  CAST(ADD_MONTHS(TIMESTAMP'2014-01-30 08:16:32', 1) AS TIMESTAMP) -- works
FROM DUAL



回答4:


SELECT current_timestamp + INTERVAL '2' MONTH from dual;

To display this in your desired format, use TO_CHAR:

SELECT TO_CHAR(current_timestamp + INTERVAL '2' MONTH, 
       'YYYY-MM-DD HH24:MI:SS.FF9TZH:TZM') from dual;
2013-03-11 23:58:14.789501000+01:00



回答5:


For Oracle:

select TO_TIMESTAMP(Sysdate,'DD-Mon-YYYY HH24-MI-SS') + 60
from dual;

select sysdate + interval '2' month from dual;

select TO_TIMESTAMP (Sysdate + interval '2' month,  'DD-Mon-YYYY HH24-MI-SS')
from dual
;

Result1:

| TO_TIMESTAMP(SYSDATE,'DD-MON-YYYYHH24-MI-SS')+60 |
----------------------------------------------------
|                     March, 12 0013 00:00:00+0000 |

Result2:

|     SYSDATE+INTERVAL'2'MONTH |
--------------------------------
| March, 11 2013 21:41:10+0000 |

Result3:

| TO_TIMESTAMP(SYSDATE+INTERVAL'2'MONTH,'DD-MON-YYYYHH24-MI-SS') |
------------------------------------------------------------------
|                                   March, 11 0013 00:00:00+0000 |
  • sqlffidle demo.


来源:https://stackoverflow.com/questions/14286877/add-2-months-to-current-timestamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!