-3

My table:

enter image description here

When I convert it to time, it fails while selecting from table:

enter image description here

But, when I test it in string, then there is no issue. What is the problem? I don't know.

enter image description here

  • 1
    Why are you storing time in a varchar? Read Aaron Bertrand's [Bad habits to kick : choosing the wrong data type](https://sqlblog.org/2009/10/12/bad-habits-to-kick-choosing-the-wrong-data-type) – Zohar Peled May 10 '18 at 06:25

2 Answers2

2

I think you have new line in your time column. Please use substring to get exact time.

select CONVERT(time, substring(timout, 1, 5), 104) 
from tbl_test

Convert will create problem when time is null or invalid so use Len function.

select CONVERT(time, substring(timout, 1, 5), 104) 
from tbl_test  
where len(TimOut) > 4
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
2
select CONVERT(time, substring((CASE TimOut WHEN '0' THEN '00:00' ELSE TimOut END),1,5),104) from tbl_test