format interval with to_char

前端 未结 6 704
迷失自我
迷失自我 2020-11-28 14:48

Following SQL command

select TO_CHAR(NVL(arg1 - arg2, TO_DSINTERVAL(\'0 00:00:00\'))) from table1

produces a result of the format: +0000000

6条回答
  •  忘掉有多难
    2020-11-28 15:26

    Slight case of thread necromancy, however I came across this question while searching for how to format an interval, so I thought it was worth adding this comment.

    From the Oracle documentation, adding a timestamp to an interval results in a timestamp, so by adding a constant timestamp with zero time elements you can then use the standard to_char format elements for datetime ...

    SELECT TO_CHAR( TIMESTAMP'1969-12-31 00:00:00' + TO_DSINTERVAL('0 00:03:01.954321'),
      'HH24:MI:SS.FF3' ) FROM dual;
    

    However, there is an issue if you intervals could be greater than a day. There is no format element for days that will yield 0. "DDD" is day of the year, so would be 365 in the example above, or 1 or more if the interval was greater then a day. This is fine as long as your intervals are less than 24 hours though.

    Should add this is on 11g so may well not have be applicable to the OP.

提交回复
热议问题