How to write bigint (timestamp in milliseconds) value as timestamp in postgresql

后端 未结 3 1708
再見小時候
再見小時候 2021-02-07 10:44

I\'m trying to store in timestamp with timezone field my value. It is in milliseconds from 1970.

select TO_CHAR(TO_TIMESTAMP(1401432881230), \'DD/MM/YYYY HH24:MI:S

3条回答
  •  长情又很酷
    2021-02-07 10:54

    This is how I convert ms to timestamp and keep ms instead seconds.The accepted answer will drop ms.

    WITH ts AS (SELECT 1401432881230 AS ts)
    SELECT to_timestamp(ts / 1000) + ((ts % 1000 ) || ' milliseconds') :: INTERVAL
    FROM ts;
    
    -- FOR ALTER COLUMN
    ALTER TABLE  my_info
      ALTER COLUMN tstmp TYPE TIMESTAMP USING to_timestamp(tstmp / 1000) + ((tstmp % 1000) || ' milliseconds') :: INTERVAL;
    

提交回复
热议问题