Convert timestamp column values to epoch in PostgreSQL select query

后端 未结 1 1788
名媛妹妹
名媛妹妹 2021-01-22 07:16

I need to convert timestamp values to epoch in a SELECT query. Please find the below sample table and expected result.

Sample table:

select          


        
相关标签:
1条回答
  • 2021-01-22 07:23

    Use the extract() function:

    select id, extract(epoch from time) as time, 
           data1, data2,data3
    from log;
    

    Apparently your column is not a timestamp column but a varchar, so you first need to cast that to a real timestamp before you can use extract()

    select id, 
           extract(epoch from time::timestamp) as time, 
           data1, data2,data3
    from log;
    

    This will only work if all values in that column have the correct ISO format for a timestamp.


    This teaches you, that you should

    never store date, timestamp or time values in a varchar column!

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