0

I have a column of type varchar with a time on it like 120217. I would like to convert this to a time format I can query. Would something like

alter table public.table alter column col_time type date using to_date(col_time, 'HHMMSS');

work?

1
  • does this work as meant alter table public.table alter column col_time type date using (col_time)::time; ?.. Commented Feb 7, 2017 at 16:34

1 Answer 1

2

this should do:

alter table public.table 
alter column col_time 
type time using (col_time::time);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! May I ask what's wrong with the solution I posted, just to learn of the difference?
to tell that I have to see error or output of your statement. I just decided to use implicit cast. to_date returns date - I suppose it just ignores your HHMMSS mask: postgresql.org/docs/current/static/functions-formatting.html
your solution gives me ERROR: result of USING clause for column "col_time" cannot be cast automatically to type date HINT: You might need to add an explicit cast.
you probaby have int in col_time then. but then you should have error when trying to run to_date(int, text)... anyway - updated answer with explicit cast
@DervinThunk - obviously no implicit conversion date to time. Missed it because did not mock up column. Thank you for edit!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.