Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert time to seconds in PostgreSQL

Tags:

I have a time value 04:30:25 that I want to convert to seconds. Is there any dedicated function to do this?

I know that we can extract hours, minutes and seconds, then calculate the seconds.

SELECT EXTRACT(hour FROM t)*60*60        + EXTRACT(minutes FROM t)*60        + EXTRACT(seconds FROM t)   FROM test;  

But I want some other way...

like image 729
Pavunkumar Avatar asked May 12 '10 06:05

Pavunkumar


People also ask

How do I get the difference in time in PostgreSQL?

Discussion: To calculate the difference between the timestamps in PostgreSQL, simply subtract the start timestamp from the end timestamp. Here, it would be arrival - departure . The difference will be of the type interval , which means you'll see it in days, hours, minutes, and seconds.

How do I convert datetime to date in PostgreSQL?

The TO_DATE function in PostgreSQL is used to converting strings into dates. Its syntax is TO_DATE(text, text) and the return type is date. The TO_TIMESTAMP function converts string data into timestamps with timezone. Its syntax is to_timestamp(text, text) .

What is epoch in PostgreSQL?

Posted on 5th September 2022. YES, you can convert EPOCH to Timestamp by merely switching to the present Timestamp in PostgreSQL DBMS. EPOCH time is nothing but the number of seconds from 00:00:00 UTC on 1 January 1970. Till date, without adding the extra leap year days, this is considered.


1 Answers

Have you tried using:

SELECT EXTRACT(EPOCH FROM INTERVAL '04:30:25'); 

If that doesn't work you could try to prefix your time value with '1970-01-01' and try:

SELECT EXTRACT(EPOCH FROM TIMESTAMP '1970-01-01 04:30:25'); 

Not tested but it seems these are your only options. Probably.

like image 149
zaf Avatar answered Sep 29 '22 21:09

zaf