How to convert Milliseconds to Date in DB2?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinodtr
    New Member
    • Dec 2006
    • 1

    How to convert Milliseconds to Date in DB2?

    Hi all,

    I have a date which is represented by the number of milliseconds since January 01, 1970. I need to change this back to DATE format. Is there any built in function in DB2 to do the needed conversion ?

    In Oracle i was able to do the conversion by the following statement.

    select to_char(to_date ('01/01/1970','mm/dd/yyyy'),'mm/dd/yyyy') + milliseconds/86400000) from dual;

    Note: In the above sql statement, milliseconds is the no: of milliseconds of the desired date.
  • bousch
    New Member
    • Mar 2008
    • 1

    #2
    In DB2 it would be:

    Code:
    SELECT
      DATE(
        TIMESTAMP('1970-01-01','00.00.00')
        + (millisecondfield/1000) seconds     )
    from ...
    Originally posted by vinodtr
    Hi all,

    I have a date which is represented by the number of milliseconds since January 01, 1970. I need to change this back to DATE format. Is there any built in function in DB2 to do the needed conversion ?

    In Oracle i was able to do the conversion by the following statement.

    select to_char(to_date ('01/01/1970','mm/dd/yyyy'),'mm/dd/yyyy') + milliseconds/86400000) from dual;

    Note: In the above sql statement, milliseconds is the no: of milliseconds of the desired date.
    Last edited by docdiesel; Mar 6 '08, 10:32 PM. Reason: Added code tags

    Comment

    • docdiesel
      Recognized Expert Contributor
      • Aug 2007
      • 297

      #3
      Hi,

      if you need a DATE value, you could calculate directly in days, too:

      Code:
      SELECT
        ( date('1970-01-01')  +
          (millisecondfield / 86400000) days
        ) as calculated_date
      FROM
        ...
      If you should try both, please let us know which one works faster.

      Regards,

      Bernd

      Comment

      Working...