Joanisc
Joanisc

Reputation: 85

How to convert from CHAR to TIME in postgresql

I have a table with a column defined as time CHAR(6) with values like '18:00' which I need to convert from char to time.

I searched here, but didn't succeed.

Upvotes: 3

Views: 3939

Answers (3)

Frank Heikens
Frank Heikens

Reputation: 127486

As said, you could use :: to cast, but you could also use the standard CAST() function:

SELECT CAST(my_column AS time) AS my_column_time
FROM   my_table;

This also works in other databases, not just PostgreSQL.

Upvotes: 1

user330315
user330315

Reputation:

If the value really is a valid time, you can just cast it:

select '18:00'::time

Upvotes: 2

Mureinik
Mureinik

Reputation: 312219

You can use the :: syntax to cast the value:

SELECT my_column::time
FROM   my_table

Upvotes: 4

Related Questions