我有一个float
数据类型的列。包含bigint
值的错误数据。值为-4.66606E+22
。
我尝试了以下逻辑,但无法正常工作。
使用case语句检查列是否在float最小和最大范围之间,如果不是,则将其更改为0。
ISNULL(TRY_CAST(Column1 AS FLOAT),0),但这不起作用,因为我们是Azure数据仓库。
Select
case
when Column1 > '1.79E+32' then 0
when Column1 < '-1.79E+32' then 0
Else Column1
End as Column1
From Table1
我也尝试过
Select
case
when Column1 between '1.79E+32' and '-1.79E+32' then Column1
Else 0
End as Column1
From Table1
期望将-4.66606E+22
替换为0
。
答案 0 :(得分:0)
bigint
的范围是-2^63 (-9,223,372,036,854,775,808)
至2^63-1 .(9,223,372,036,854,775,807)
。
并且float
的范围是- 1.79E+308
至-2.23E-308
,0
和2.23E-308
至1.79E+308
参考:
bigint
数据-4.66606E+22
在浮动范围内。这就是为什么您的代码不起作用的原因。正如HABO所说,将浮点数与字符串进行比较没有好处,例如
您想将-4.66606E + 22替换为0,也许您可以尝试:
Select
case Column1
when -4.66606E+22 then 0
End
From Table1
希望这可以为您提供帮助。
答案 1 :(得分:0)
让我们从BigInt
范围开始:-2 ^ 63(-9,223,372,036,854,775,808)到2 ^ 63-1(9,223,372,036,854,775,807)。那大约是±9E + 18,所以-4.66606E + 22不可能是BigInt
值。
继续比较不同数据类型的值:
declare @Foo as BigInt = 8765432109876543210;
declare @FloatFoo as Float(24) = @Foo, @DoubleFoo as Float(53) = @Foo;
-- According to the rules for data type preedence a BigInt value will be converted to a Float
-- when the two data types are compared. Hence all of the values are "equal".
select @Foo as Foo,
@FloatFoo as FloatFoo, case when @Foo = @FloatFoo then 1 else 0 end as FloatMatch,
@DoubleFoo as DoubleFoo, case when @Foo = @DoubleFoo then 1 else 0 end as DoubleMatch;
-- Since a Float(53) has a precision of 15 digits that means some precision will be lost.
set @Foo += 1; -- Bump the BigInt value, but leave the Floats unchanged.
-- And ... the values are all still "equal"!
select @Foo as Foo,
@FloatFoo as FloatFoo, case when @Foo = @FloatFoo then 1 else 0 end as FloatMatch,
@DoubleFoo as DoubleFoo, case when @Foo = @DoubleFoo then 1 else 0 end as DoubleMatch;
-- Once more, with feeling.
set @Foo += 1000; -- A bigger bump makes the Float(53) value different, but not the Float(24).
select @Foo as Foo,
@FloatFoo as FloatFoo, case when @Foo = @FloatFoo then 1 else 0 end as FloatMatch,
@DoubleFoo as DoubleFoo, case when @Foo = @DoubleFoo then 1 else 0 end as DoubleMatch;
引用:Data type precedence,Float
。
执行摘要:“存在一个错误数据,其值为bigint
。值为-4.66606E+22
。”那是真的坏数据。比较BigInt
和Float
的值是不精确的活动,很可能会导致失望。