Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Hours and Minutes (HH:MM) from date

I want to get only hh:mm from date.

How I can get this?

I have tried this :

CONVERT(VARCHAR(8), getdate(), 108) 
like image 707
Rajbir Singh Avatar asked Jan 17 '13 08:01

Rajbir Singh


People also ask

How do you calculate hours and minutes from dates?

To get the hours and minutes from a date, call the getHours() and getMinutes() methods on the date object. The getHours method returns the hours and the getMinutes() method returns the minutes in the specified date.

How do I get HH MM from datetime in SQL?

By using format code as 106 we can get datetime in “DD MMM YYYY” format. By using format code as 107 we can get datetime in “MMM DD, YYYY” format. By using format code as 108 we can get datetime in “HH:mm: ss” format.

How do I get only hours and minutes from datetime in SQL?

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.

How do I get hours and minutes from timestamp in SQL?

The time (hour, minute or second) components can be found by either: Using CAST( datevalue AS TIMESTAMP ) to convert the DATE to a TIMESTAMP and then using EXTRACT( [ HOUR | MINUTE | SECOND ] FROM timestampvalue ) ; or. Using TO_CHAR( datevalue, format_model ) to get the value as a string.


2 Answers

Just use the first 5 characters...?

 SELECT CONVERT(VARCHAR(5),getdate(),108)  
like image 167
gbn Avatar answered Oct 08 '22 04:10

gbn


You can easily use Format() function instead of all the casting for sql 2012 and above only

SELECT FORMAT(GETDATE(),'hh:mm') 

This is by far the best way to do the required conversion.

like image 38
Abdul Hannan Ijaz Avatar answered Oct 08 '22 04:10

Abdul Hannan Ijaz