1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
John_Snow
  • 62
  • 1
  • 1
  • 8

4 Answers4

3

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

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
2
SELECT FORMAT(cast('2017-08-17 15:31:18.217' as datetime),'hh:mm')
piyush jain
  • 113
  • 8
  • 5
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion – doydoy Aug 17 '17 at 11:14
0

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))
Gagan Sharma
  • 220
  • 1
  • 7
  • @Loki , Please execute these 2 queries , you will get the hours and minutes of current time. That's the requirement. SELECT GETDATE() SELECT CONVERT(VARCHAR(5), CAST(GETDATE() AS TIME)) – Gagan Sharma Aug 17 '17 at 11:47
  • Please edit this to the answer. Thus, future users can understand. Also, have a look at [how to answer](https://stackoverflow.com/help/how-to-answer) – loki Aug 17 '17 at 11:48
  • @Loki , Done, Thanks – Gagan Sharma Aug 17 '17 at 11:51
0
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.

Horia
  • 1,612
  • 11
  • 18