Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I subtract two times on Postgres if they are stored as character varying?

I have a table that has stored columns time_attended and time_departed from a site.

for example time_attended = "16:00" and time_departed = "17:00"

They are stored as character varying fields – how would I find the time difference?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Doing a conversion eg –
SELECT
time_attended::date – time_departed::date
FROM table

throws up the syntax error ‘invalid input syntax for type date: "16:00"’

Any ideas? thanks

>Solution :

If those are time values, cast them to a time not a date

select time_attended::time - time_departed::time
from the_table
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading