Reputation: 62
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
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
Reputation: 113
SELECT FORMAT(cast('2017-08-17 15:31:18.217' as datetime),'hh:mm')
Upvotes: 2
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
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