Smu*_*ger 9 sql-server type-conversion
我的字段值产品长度为0.123.这是一个视图,数据类型为varchar.
我需要将它转换为浮点数或数值,以便执行数学比较.
convert(float,productlength)和cast(productlength as float)都不起作用.
error varchar cant be converted to float
或者说有些事.
从我所读的varchar可以简单地不能转换为数字字符串?
有什么聪明的方法吗?
Bil*_*egg 19
您可以将varchars转换为浮点数,并且可以按照表达的方式执行.您的varchar不能是数值.必须有其他东西.您可以使用IsNumeric进行测试.看到这个:
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)
Run Code Online (Sandbox Code Playgroud)
小智 6
使用
Try_convert(float,[Value])
Run Code Online (Sandbox Code Playgroud)
参见 https://raresql.com/2013/04/26/sql-server-how-to-convert-varchar-to-float/
归档时间: |
|
查看次数: |
129732 次 |
最近记录: |