解析时将数据类型 varchar 转换为 float 时出错
Error converting data type varchar to float when parsing
我创建了一个将解析原始数据的存储过程。下面的代码是存储过程的一部分。我正在测试它 returns 一个错误 'Error converting data type varchar to float.'。如果您注意到我在 case 和 else 中都更改了 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.
你在这里遇到了双重问题。
让我们拿一段代码
--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,
您需要更改以下两项中的一项。 IF
到 NULL
中的 'NULL'
或数值,或
在你的声明中 ELSE
应该 return 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
return 相同的数据类型,并且永远不会命中 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)
它将 return .8,01004000
现在,您将此结果包含在 cast
- cast('.8,01004000' as float)
中
你的问题 - “.8,01004000”看起来不像是 float 的字符串表示形式。
现在,检查一下
select CAST('.8,01004000' as float)
- 不工作(将数据类型 varchar 转换为 float 时出错)
select CAST('.801004000' as float)
- 工作
错误出在您对 [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,
...
我创建了一个将解析原始数据的存储过程。下面的代码是存储过程的一部分。我正在测试它 returns 一个错误 'Error converting data type varchar to float.'。如果您注意到我在 case 和 else 中都更改了 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.
你在这里遇到了双重问题。 让我们拿一段代码
--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,
您需要更改以下两项中的一项。 IF
到 NULL
中的 'NULL'
或数值,或
在你的声明中 ELSE
应该 return 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
return 相同的数据类型,并且永远不会命中 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)
它将 return .8,01004000
现在,您将此结果包含在 cast
- cast('.8,01004000' as float)
你的问题 - “.8,01004000”看起来不像是 float 的字符串表示形式。
现在,检查一下
select CAST('.8,01004000' as float)
- 不工作(将数据类型 varchar 转换为 float 时出错)
select CAST('.801004000' as float)
- 工作
错误出在您对 [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,
...