Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to timestamp in Postgresql

Problem

How do you convert a string to a timestamp?

The documentation and all answers that I have found show how to convert at string column to timestamp with the to_timestamp function, but this apparently does not work for a single string.

What I have tried

to_timestamp('2019-09-20 13:59', 'DD-MM-YYYY HH24:MI:SS')

Cast('2019-09-20 13:59' as timestamp)

What I want to do

I want to add a column to a table with this date as a repeated value.

-- Creating timestamp column
ALTER TABLE my_table ADD creation_date timestamp

-- Repeating timestamp 
UPDATE my_table  SET creation_date = TO_TIMESTAMP('2019-09-20 13:59', 'YYYY-MM-DD HH24:MI')
like image 566
Esben Eickhardt Avatar asked Sep 03 '25 05:09

Esben Eickhardt


1 Answers

You can try below - change your format mask from 'DD-MM-YYYY HH24:MI:SS' to 'YYYY-MM-DD HH24:MI:SS'

DEMO

select to_timestamp('2019-09-20 13:59', 'YYYY-MM-DD HH24:MI:SS')
like image 192
Fahmi Avatar answered Sep 04 '25 18:09

Fahmi