oracle to_date conversion showing literal does not match string format

后端 未结 2 1199
深忆病人
深忆病人 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:20

    TO_DATE method tries to match the input with the provided pattern. For ex:

    TO_DATE('2003/07/09', 'yyyy/mm/dd')
    TO_DATE('070903', 'MMDDYY')
    TO_DATE('20020315', 'yyyymmdd')
    

    you can check http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions183.htm

    I guess this will help you (haven't tested)

    SELECT TO_CHAR(TO_DATE('01-JAN-1970','DD-MON-YYYY HH24:MI:SS') + 
    ( 1117556597/ (1000*60 * 60 * 24) ),'DD-MON-YYYY HH24:MI:SS') "DATE" FROM DUAL ;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题