Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Time from Datetime SQL

I want to get only Time from a DateTime column using SQL query using SQL Server 2012:

The datetime format in the column at the moment is:

dd/mm/yyyy hh:mm

example - 16/10/2018 11:50

I want to extract the hh:mm and convert the column to a time format (hh:mm or hh:mm:ss)

Any assistance would be much appreciated.

Thanks.

like image 675
Beanymattd Avatar asked Dec 06 '18 12:12

Beanymattd


2 Answers

do cast

select cast(datecol as time) [time]
from t
like image 107
Zaynul Abadin Tuhin Avatar answered Oct 06 '22 16:10

Zaynul Abadin Tuhin


If the column is a string, do you simply want this?

select convert(time, right(col, 5))
like image 28
Gordon Linoff Avatar answered Oct 06 '22 16:10

Gordon Linoff