1

I have a pretty standard date as a string that I need to parse to a date type:

"2016-06-01T23:34:25+00:00"

I'm using 'YYYY-MM-DDTHH24:MI:SS' as the format mask and this query to try to get and parse the date:

SELECT to_date('last_updated_on', 'YYYY-MM-DDTHH24:MI:SS') 
AS last_updated_on FROM locations 
limit 1

What happens is I get this error:

ERROR: invalid value ":2" for "MI"
SQL state: 22007
Detail: Value must be an integer.

I've looked all over in the documentation and on SO trying to find out why this is happening, and I'm totally baffled.

I'm using PostgreSQL 9.4

2 Answers 2

6

Surround the T in format string with "". If the time part of the date needs to be preserved, use to_timestamp().

SELECT to_date('last_updated_on', 'YYYY-MM-DD"T"HH24:MI:SS') 
AS last_updated_on FROM locations 
limit 1

Converting to date with timestamp

SELECT to_timestamp('last_updated_on', 'YYYY-MM-DD"T"HH24:MI:SS') 
AS last_updated_on FROM locations 
limit 1
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I suspected that T was the culprit, but didn't know how to define it as a literal "T"
4

The problem would appear to be the T n the middle of the string. This seems tow work:

SELECT to_date(replace('2016-06-01T23:34:25+00:00', 'T', ''),  'YYYY-MM-DD HH24:MI:SS')

However, you probably want to_timestamp():

SELECT to_timestamp('2016-06-01T23:34:25+00:00', 'YYYY-MM-DD HH24:MI:SS')

Also note that you have single quotes around last_updated_on. That means that it is interpreted as a string, not a column name.

1 Comment

Awesome. Thanks! Yeah I put that last_updated_on in when writing my question. The value that goes in there comes from a jsonb value, which I took out to try to clarify the meaning of the question.

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.