将varchar(max)转换为int或float时出错

won*_*t77 1 sql t-sql casting sql-server-2005

我有一个表,将不同的信息存储到值列中 varchar(max)

我需要能够从该表中提取一些信息,将其转换为整数并对数字求平均值.我试图转换时遇到了一个问题.

这不起作用:

select cast(value as float) as value
from table
Run Code Online (Sandbox Code Playgroud)

谁能告诉我如何正确转换这个?

Gor*_*off 5

据推测,问题是某些值不是数字格式.试试这个:

select (case when isnumeric(value) = 1 then cast(value as float) end)
from table
Run Code Online (Sandbox Code Playgroud)

这会将所有数字转换为float,并将NULL放在其余字段中.

如果要查看导致问题的值,请使用以下命令:

select value
from table
where isnumeric(value) = 0 and value is not null
Run Code Online (Sandbox Code Playgroud)