Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Conversion between INTERVAL YEAR TO MONTH and INTERVAL DAY TO SECOND

PuraVidaOTNMar 26 2013 — edited Mar 26 2013
(1)

Oracle document states that the difference between two timestamp values is INTERVAL, but it did not say
it is INTERVAL YEAR TO MONTH or INTERVAL DAY TO SECOND. How do we know and can rely on that
it is INTERVAL(9) TO SECOND(9)?

(2)

Suppose I have this table and 1 row in the table:
create table test_ts (
    a timestamp,
    b timestamp,
    c INTERVAL YEAR TO MONTH
)
;

insert into test_ts (
    a,
    b
)
values (
    current_timestamp,
    current_timestamp - INTERVAL '1234 11:22:33.567890' DAY ( 4) TO SECOND ( 6)
)
;
I want to update column c with the difference of column a and b. The best way I can come up with is this:
update test_ts set c = to_yminterval (
    extract ( year  from ( a - b) year to month) || '-' ||
    extract ( month from ( a - b) year to month)
)
;
Is there a better way? The doesn't CAST does not work as written like this:
select cast(a-b as interval year to month) from test_ts;
(3)

Suppose later I do want precision, so I added another column:
alter table test_ts add (
    d INTERVAL DAY ( 4) TO SECOND
)
;
and assuming a and b columns were dropped c were computed:
alter table test_ts drop column a;
alter table test_ts drop column b;
and I want to update d with c. I cannot simple set d to c, again because days and seconds cannot converted
years and months without a reference point. The best I can come up with this which uses a current_timestamp
as a reference point.
update test_ts set d = c + current_timestamp - current_timestamp;
Is there a better way?

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 23 2013
Added on Mar 26 2013
1 comment
1,034 views