我创建了一个解析原始数据的存储过程。下面的代码是存储过程的一部分。我正在测试它并返回错误'将数据类型varchar转换为float时出错。'。如果您注意到我在KM2中更改了原始数据,无论是在案例还是在其他地方。
有人可以帮助我如何做到正确,提前感谢所有帮助人员。
SELECT [Account] = LEFT('351856040520298,241111;1G,141007034741,A,1430.4280N,12100.0551E,0.0,330,0.8,20000000;2G,141007034741,11,22.8,12.87,04.09,273342044.3',CHARINDEX(',','351856040520298,241111;1G,141007034741,A,1430.4280N,12100.0551E,0.0,330,0.8,20000000;2G,141007034741,11,22.8,12.87,04.09,273342044.3') - 1),
[TrxDateTime] = cast(stuff(stuff(stuff((SUBSTRING('351856040520298,241111;1G,141007034741,A,1430.4280N,12100.0551E,0.0,330,0.8,20000000;2G,141007034741,11,22.8,12.87,04.09,273342044.3', 27, 12)), 7,0,' '), 10,0,':'), 13,0,':') as datetime),
[PhilTime] = GETDATE(),
[GPSStatus] = CASE WHEN(SUBSTRING('351856040520298,241111;1G,141007034741,A,1430.4280N,12100.0551E,0.0,330,0.8,20000000;2G,141007034741,11,22.8,12.87,04.09,273342044.3', 40, 1) = 'A')
THEN 'OK'
ELSE 'Message Not Valid' END,
[Lat] = cast(SUBSTRING('351856040520298,241111;1G,141007034741,A,1430.4280N,12100.0551E,0.0,330,0.8,20000000;2G,141007034741,11,22.8,12.87,04.09,273342044.3', 42, 9) as float)/100,
[Long] = cast(SUBSTRING('351856040520298,241111;1G,141007034741,A,1430.4280N,12100.0551E,0.0,330,0.8,20000000;2G,141007034741,11,22.8,12.87,04.09,273342044.3', 53, 10) as float)/100,
[Speed2] = cast(SUBSTRING('351856040520298,241111;1G,141007034741,A,1430.4280N,12100.0551E,0.0,330,0.8,20000000;2G,141007034741,11,22.8,12.87,04.09,273342044.3', 65, 1) as float)*1.852/1000,
[ENGINE] = CASE WHEN (SUBSTRING('351856040520298,241111;1G,141007034741,A,1430.4280N,12100.0551E,0.0,330,0.8,20000000;2G,141007034741,11,22.8,12.87,04.09,273342044.3', 77, 1) = '2')
THEN 'ON'
ELSE 'OFF' END,
[KM2] = CASE WHEN(right('351856040520389,241111;1G,141209203228,A,1437.3980N,12103.6893E,0.0,44,0.8,01004000', 11) like '%,%')
THEN 'NULL'
--ELSE 'BDADADADAD' END,
ELSE round(cast(right('351856040520389,241111;1G,141209203228,A,1437.3980N,12103.6893E,0.0,44,0.8,01004000', 11)as float)/3600, 0, 1) END,
[PlateNo] = NULL,
[ProcStatus] = '0';
我一直遇到的错误是
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.
答案 0 :(得分:3)
错误在您[KM2]的定义中。根据以下内容将'NULL'取消引用为NULL来更改它:
...
[KM2] = CASE WHEN(right('351856040520389,241111;1G,141209203228,A,1437.3980N,12103.6893E,0.0,44,0.8,01004000', 11) like '%,%')
THEN NULL
ELSE round(cast(right('351856040520389,241111;1G,141209203228,A,1437.3980N,12103.6893E,0.0,44,0.8,01004000', 11)as float)/3600, 0, 1) END,
...
答案 1 :(得分:1)
你在这里遇到了双重问题。 让我们看一段你的代码
--ELSE 'BDADADADAD' END,
ELSE
round(cast(right('351856040520389,241111;1G,141209203228,A,1437.3980N,12103.6893E,0.0,44,0.8,01004000', 11)as float)/3600, 0, 1) END,
你需要改变两件事之一。 'NULL'
到IF
中的NULL
或数值,或者
您ELSE
中的陈述应返回Varchar
。但是如果你的ELSE
会以当前的形式出现,那么由于我在下面解释的内容,它仍然是一个问题。例如,这有效:
select CASE WHEN(right('351856040520389,241111;1G,141209203228,A,1437.3980N,12103.6893E,0.0,44,0.8,01004000', 11) like '%,%')
THEN 'Null'
ELSE cast(round(cast(right('351856040520389,241111;1G,141209203228,A,1437.3980N,12103.6893E,0.0,44,0.8,01004000', 11)as float)
/3600, 0, 1)as varchar) END
它有效,因为if
和else
返回相同的数据类型,else
永远不会被点击。检查一下 - 这也有效:
select CASE WHEN(right('351856040520389,241111;1G,141209203228,A,1437.3980N,12103.6893E,0.0,44,0.8,01004000', 11) like '%,%')
THEN 1
ELSE cast(cast(right('351856040520389,241111;1G,141209203228,A,1437.3980N,12103.6893E,0.0,44,0.8,01004000', 11)as float)
/3600, 0, 1) END
现在。看看这部分:right('351856040520389,241111;1G,141209203228,A,1437.3980N,12103.6893E,0.0,44,0.8,01004000', 11)
它将返回.8,01004000
现在,您将此结果括在cast
- cast('.8,01004000' as float)
你的问题 - " .8,01004000"看起来不像float的字符串表示。
现在,看看这个
select CAST('.8,01004000' as float)
- 不工作(将数据类型varchar转换为float时出错)
select CAST('.801004000' as float)
- 工作