1

Question itself was asked multiple times but I happened to come across a problem I can't fix.

I Have a table containing dates in format: Tue Jan 29 15:24:20 CET 2019 which according to fields in documentation stands for Dy Mon DD HH24:MI:SS TZ YYYY.

updated column in table is type of text

I would like to update each row and convert this date to YYYY-MM-DD HH24:MI:SS but first I want to convert the date from one format to the other however I got sql error:

select to_char(to_date(updated, 'Dy Mon DD HH24:MI:SS TZ YYYY'), 'YYYY-MM-DD HH24:MI:SS') from audit where id =1267;
ERROR:  formatting field "TZ" is only supported in to_char

Can anyone give me a hint how to do so?

6
  • What is the datatype of column updated ? Commented Feb 14, 2019 at 8:21
  • updated question. Type is text Commented Feb 14, 2019 at 8:23
  • 1
    Never store date (or timestamp) values in text or varchar columns. If you had used a proper date data type, you could simply use to_char() to display the date in any format you wish. Commented Feb 14, 2019 at 8:25
  • @a_horse_with_no_name dont mention it to me.. I just happened to deal with the database "as it is".. Ofc no -XL - fixed link to doc Commented Feb 14, 2019 at 8:27
  • 2
    What happens if you simply do cast(updated as timestamp)? Commented Feb 14, 2019 at 8:31

1 Answer 1

3

One problem with your code is that to_date returns a date only (without a time). to_timestamp would be more appropriate.

But even then the documentation says:

TZ - upper case time-zone abbreviation (only supported in to_char)

But the documentation also says:

Tip:
to_timestamp and to_date exist to handle input formats that cannot be converted by simple casting. For most standard date/time formats, simply casting the source string to the required data type works, and is much easier.

So what you should do instead is to use a cast:

cast(updated as timestamp with time zone)

Or, if you want to ignore the timezone:

cast(updated as timestamp)
Sign up to request clarification or add additional context in comments.

Comments

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.