How can I have the right format of time with SQL Server?

Viewed 38

Hello I have this column :

date1
6/19/2019 3:10:12 PM

I wanted just to extract the time so I used the following query :

select try_parse([date1] as time using 'en-US') from myTable

But the problem is I got this :

15:10:12.0000000

whereas I would like to just have this :

15:10:12

How can I solve this ?

Thank you very much !

2 Answers

You can try

SELECT convert(varchar(max), try_parse('6/19/2019 3:10:12 PM' AS time USING 'en-US'), 108)
       FROM mytable;

if you want the result as a string for displaying purposes. (But you shouldn't store a date/time as string. Use the proper data types.)

You can use FORMAT:

SELECT FORMAT(try_parse([date1] as datetime using 'en-US'), 'HH:mm:ss') from myTable;
Related