mssql 将 varchar 转换为 float

mssql convert varchar to float(mssql 将 varchar 转换为 float)
本文介绍了mssql 将 varchar 转换为 float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字段值 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)