oracle to_date conversion showing literal does not match string format

后端 未结 2 1224
深忆病人
深忆病人 2021-01-25 21:41

If I use a converter for unixtime I get

Tue, 31 May 2005 16:23:17 GMT for 1117556597. If I run the below query, I get the error literal does not match string format. Wh

2条回答
  •  耶瑟儿~
    2021-01-25 22:26

    19700101 is January 1, 1970. This is the "epoch" for UNIX systems. UNIX systems keep track of time by counting the number of seconds since the "epoch".

    You need to convert it like this,

    SQL> SELECT TO_DATE('19700101','yyyymmdd')  + (1117556597/24/60/60) thedate
      2  FROM dual
      3  /
    
    THEDATE
    ---------
    31-MAY-05
    

    Lets see the complete datetime,

    SQL> SELECT to_char(TO_DATE('19700101','yyyymmdd')  + (1117556597/24/60/60),'DD-MON-YYYY HH24:MI:SS') thedate
      2  FROM dual
      3  /
    
    THEDATE
    --------------------
    31-MAY-2005 16:23:17
    
    SQL>
    

    EDIT Regarding the TIMEZONE

    It is better to explicitly mention the timezone.

    You could mention the timezone in the literal itself, or cast it as UTC and convert it to your local timezone.

    SELECT
      TO_CHAR (
        FROM_TZ (
          CAST (DATE '1970-01-01 + (1117556597/24/60/60) AS TIMESTAMP),
          'UTC')
        AT TIME ZONE 'your local timezone',
      'MM/DD/YYYY HH24:MI:SS')
    FROM dual;
    

    NOTE Please check syntax, I don't have DB access while making this edit.

提交回复
热议问题