Sql server 在SQL Server 2014中将varchar转换为时间失败
我的桌子: 将其转换为时间时,从表中选择时失败: 但是,当我在字符串中测试它时,就没有问题了。有什么问题?我不知道Sql server 在SQL Server 2014中将varchar转换为时间失败,sql-server,time,varchar,Sql Server,Time,Varchar,我的桌子: 将其转换为时间时,从表中选择时失败: 但是,当我在字符串中测试它时,就没有问题了。有什么问题?我不知道 我想你的《时代》专栏里有新行。请使用子字符串获取准确时间 select CONVERT(time, substring(timout, 1, 5), 104) from tbl_test 当时间为空或无效时,Convert将产生问题,因此请使用Len函数 select CONVERT(time, substring(timout, 1, 5), 104) from tbl_t
我想你的《时代》专栏里有新行。请使用子字符串获取准确时间
select CONVERT(time, substring(timout, 1, 5), 104)
from tbl_test
当时间为空或无效时,Convert将产生问题,因此请使用Len函数
select CONVERT(time, substring(timout, 1, 5), 104)
from tbl_test
where len(TimOut) > 4
为什么要把时间存储在varchar中?阅读Aaron Bertrand的if length大于4或小于什么?您可以使用case语句。然后使用case语句并将小于4位的时间转换为等于4位的时间。像下面的答案
select CONVERT(time, substring((CASE TimOut WHEN '0' THEN '00:00' ELSE TimOut END),1,5),104) from tbl_test