John_Snow
John_Snow

Reputation: 62

Convert to HH:MM in SQL Server

I would like to convert a date time format to hh:mm using SQL Server.

For example: convert this value 2017-08-17 15:31:18.217 into 15:31.

If anyone knows how to do this, please help.

Upvotes: 1

Views: 17855

Answers (4)

Gagan Sharma
Gagan Sharma

Reputation: 220

Please execute these 2 queries , you will get the hours and minutes of current time.

 SELECT GETDATE() 
 SELECT CONVERT(VARCHAR(5), CAST(GETDATE() AS TIME))

Upvotes: 0

piyush jain
piyush jain

Reputation: 113

SELECT FORMAT(cast('2017-08-17 15:31:18.217' as datetime),'hh:mm')

Upvotes: 2

Horia
Horia

Reputation: 1612

SELECT CONVERT(VARCHAR(5), GETDATE(), 108)

You may want to use TRY_CONVERT if supported,and you don't always have a in that column.

One thing, the value that gets converted needs to be of type DATETIME.

Upvotes: 0

Zohar Peled
Zohar Peled

Reputation: 82474

Here is one way to do it:

DECLARE @DateTime datetime = '2017-08-17 15:31:18.217'

SELECT CONVERT(char(5), @DateTime, 108)

Result: 15:31

Upvotes: 4

Related Questions