问题描述
我的字段值 productlength 为 0.123.这是一个视图,数据类型为 varchar.
I have a field value productlength of 0.123. This is from a view and has a data type of varchar.
我需要将其转换为浮点数或数值,以便进行数学比较.
I need to convert it to a float or numeric value so as o perform math comparisons.
转换(浮点数,产品长度)和cast(productlength as float) 都不起作用.
convert(float,productlength) and cast(productlength as float) both do not work.
error varchar cant be convert to float
或类似的东西.
从我读到的 varchar 根本不能转换为数字字符串?
From what I have read varchar can simply not be converted to a numeric string?
有什么巧妙的办法解决这个问题?
Any clever ways around this?
推荐答案
你可以将varchars转换为float,你可以按照你表达的方式来做.您的 varchar 不能是数值.里面一定有别的东西.您可以使用 IsNumeric 对其进行测试.看到这个:
You can convert varchars to floats, and you can do it in the manner you have expressed. Your varchar must not be a numeric value. There must be something else in it. You can use IsNumeric to test it. See this:
declare @thing varchar(100)
select @thing = '122.332'
--This returns 1 since it is numeric.
select isnumeric(@thing)
--This converts just fine.
select convert(float,@thing)
select @thing = '122.332.'
--This returns 0 since it is not numeric.
select isnumeric(@thing)
--This convert throws.
select convert(float,@thing)
这篇关于mssql 将 varchar 转换为 float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!