Sql 努力将Varchar转换为Float

Sql 努力将Varchar转换为Float,sql,sql-server,Sql,Sql Server,我在发布前用谷歌搜索了一下。看起来我下面的代码应该可以轻松地将varchar转换为float,但我一直遇到这个错误: Error converting data type varchar to float. SQL: 如果正在使用convert()或使用try\u convert(),请使用条件块 或 以下是示例代码: create table mytable(data varchar(50)); insert into mytable values(65.98); insert into

我在发布前用谷歌搜索了一下。看起来我下面的代码应该可以轻松地将varchar转换为float,但我一直遇到这个错误:

Error converting data type varchar to float.
SQL:


如果正在使用
convert()
或使用
try\u convert()
,请使用条件块

以下是示例代码:

create table mytable(data varchar(50));

insert into mytable values(65.98);
insert into mytable values(54);
insert into mytable values('xyz');
insert into mytable values(100.9876);

select data,
(case when isnumeric(data)=1 Then
convert(float,data) else null end)
from mytable;

select data, try_convert(float,data)
from mytable;
请看这里的小提琴:


注意:对于某些输入,如
isnumeric()
检查不起作用,因此建议使用
尝试转换()
解决方案。

列中有无法转换的数据。使用TRY_CAST查找它。您可以执行
从gs order by 1中选择不同的bval_bid_price,然后查看是否有任何数据无法转换-就像上面提到的选项卡一样。当bval_bid_price为null时,您还可以使用
case,然后使用null else cast(…)end as bid
将null值保持为null,而不对其进行强制转换。若要标识存储无效值的行(可以通过首先为列使用正确的数据类型来避免),请使用
SELECT*FROM dbo.BULK_GS WHERE(BVAL_ASK_PRICE不为空,TRY_CONVERT(float,BVAL_ASK_PRICE)为空)或(BVAL_BID_PRICE不为空,TRY_CONVERT(float,BVAL_BID_PRICE)为空);
isnumeric=1
并不意味着强制转换为float会起作用。例如,试试
。你有一点。我建议
TRY_CONVERT()
。这总是有效的。
select data,
(case when isnumeric(data)=1 Then
convert(float,data) else null end)
from mytable;
select data, try_convert(float,data)
from mytable;
create table mytable(data varchar(50));

insert into mytable values(65.98);
insert into mytable values(54);
insert into mytable values('xyz');
insert into mytable values(100.9876);

select data,
(case when isnumeric(data)=1 Then
convert(float,data) else null end)
from mytable;

select data, try_convert(float,data)
from mytable;