Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract hour, minutes and seconds from a DATE

Tags:

sql

postgresql

I have the following query:

select cast(max_bid_ts as TIMESTAMP) from my_table;

I cast the max_bid_ts because it was a double and I wanted to be a TMESTAMP. This query returns something like this

2016-04-21 12:41:46.313999872

I only want the hour:minutes:seconds part. In this case would be 12:41:46. What would be the best way to do this?

like image 216
danilojara123 Avatar asked Apr 25 '16 20:04

danilojara123


People also ask

How do I extract hours and minutes from datetime?

How to Get the Current Time with the datetime Module. To get the current time in particular, you can use the strftime() method and pass into it the string ”%H:%M:%S” representing hours, minutes, and seconds.

How do I extract hours and minutes from datetime in Excel?

To extract time only from datetime with formula, you just need to do as follow: 1. Select a blank cell, and type this formula =TIME(HOUR(A1),MINUTE(A1), SECOND(A1)) (A1 is the first cell of the list you want to extract time from), press Enter button and drag the fill handle to fill range.

How do I get hours minutes and seconds from date in SQL Server?

We can use DATEPART() function to get the HOUR part of the DateTime in Sql Server, here we need to specify datepart parameter of the DATEPART function as hour or hh.


1 Answers

Try something like this:

select to_char(YourTimeStampField,'HH:MI:SS') from YourTable

And if you want a 24-hour time format, try this:

select to_char(YourTimeStampField,'HH24:MI:SS') from YourTable

Another way would be:

select YourTimeStampField::time from YourTable
like image 181
cableload Avatar answered Oct 08 '22 10:10

cableload