Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the time of a datetime using T-SQL?

How to get the time for a given datetime value?

I have a datetime in database like this:

2010-09-06 17:07:28.170 

and want only the time portion:

17:07:28.170 

Is there a function for that or something?

like image 490
grady Avatar asked Sep 07 '10 07:09

grady


People also ask

How do I get timestamp from time in SQL?

getDate() is giving current date and time. 108 is formatting/giving us the required portion i.e time in this case. varchar(8) gives us the number of characters from that portion.

Does Getdate () include time?

SQL Server GETDATE() Function The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format.


2 Answers

Just to add that from SQL Server 2008, there is a TIME datatype so from then on you can do:

SELECT CONVERT(TIME, GETDATE()) 

Might be useful for those that use SQL 2008+ and find this question.

like image 85
AdaTheDev Avatar answered Sep 21 '22 04:09

AdaTheDev


In case of SQL Server, this should work

SELECT CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond 
like image 24
Jagmag Avatar answered Sep 21 '22 04:09

Jagmag