Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get hour and minute only from a DateTime

I have let's say

'2015-01-16 13:50:00.000'

in my database.

How do I get the hours and minute only ?

13:50

in the end I want to make query like this

Update A set status = 1 WHERE endTime = current_time()

*endTime = field name of my sample above.

like image 799
azalikaEriya Avatar asked Dec 25 '22 00:12

azalikaEriya


1 Answers

SELECT DATEPART(HOUR, GETDATE());
SELECT DATEPART(MINUTE, GETDATE());

MSDN: DATEPART

I want to get 13:50. is it the way

SELECT CAST(DATEPART(HOUR,   GETDATE())AS VARCHAR(2)) + ':' +
       CAST(DATEPART(MINUTE, GETDATE())AS VARCHAR(2))

I'm on SQL-Server 2005 which has no TIME datatype, but this should also work for you:

SELECT LEFT(DATEADD(MINUTE, 10, (CONVERT(TIME(0),GETDATE()))),5) 

If you want to filter your records by the hour+minute part:

WHERE DATEPART(HOUR,   endTime)= DATEPART(HOUR, GETDATE())
 AND  DATEPART(MINUTE, endTime)= DATEPART(HOUR, GETDATE())
like image 137
Tim Schmelter Avatar answered Mar 08 '23 11:03

Tim Schmelter