Saturday, September 26, 2009

ANSI date-time datatypes and arithmetic

ALTER session SET nls_date_format = 'YYYY-MM-DD';

-- old
SELECT ADD_MONTHS(TO_DATE('2003-07-31','YYYY-MM-DD'),-1) FROM dual;

ADD_MONTHS
----------
2003-06-30

-- new
SELECT TO_TIMESTAMP('2003-07-31','YYYY-MM-DD') - INTERVAL '1' MONTH FROM dual;

ERROR AT line 1:
ORA-01839: DATE NOT valid FOR MONTH specified

-- old
SELECT TO_DATE('2003-07-31','YYYY-MM-DD') - 100 FROM dual;

-- new (broken)
SELECT TO_TIMESTAMP('2003-07-31','YYYY-MM-DD') - INTERVAL '100' DAY FROM dual;

ERROR AT line 1:
ORA-01873: the leading precision OF the INTERVAL IS too small

-- new (note the extra "(3)")
SELECT TO_TIMESTAMP('2003-07-31','YYYY-MM-DD') - INTERVAL '100' DAY(3) FROM dual;

TO_TIMESTAMP('2003-07-31','YYYY-MM-DD')-INTERVAL'100'DAY(3)
-------------------------------------------------------------
2003-04-22 00:00:00

No comments: