4

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')
3
  • 1
    What exactly is your problem? The code you have shown will work just fine: rextester.com/GQC96126 Commented Sep 23, 2019 at 8:22
  • Your code works fine: dbfiddle.uk/… Commented Sep 23, 2019 at 8:24
  • The problem was that the code fails unless you write 'select' in front of it apparently. Commented Sep 23, 2019 at 8:25

1 Answer 1

7

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')
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, so the error message is because, you can not just run it without select?
Thanks! I had a case where a date column was wrongly typed TEXT with ISO 8061 values, I did this: to_timestamp(date, 'YYYY-MM-DD"T"HH24:MI:SS').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.