sql - Azure 数据仓库 - 修复 Float 数据类型列中的 bigint 值
问题描述
我有一个float
数据类型的列。有一个带有bigint
值的坏数据。值为-4.66606E+22
。
我尝试了以下逻辑,但它不起作用。
使用 case 语句检查列是否落在浮动最小和最大范围之间,如果不是,则将其更改为 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
。
解决方案
让我们从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;
执行摘要:“有一个带有bigint
值的坏数据。该值为-4.66606E+22
。” 这真是糟糕的数据。比较BigInt
和Float
价值是一种不精确的活动,可能会导致失望。
推荐阅读
- java - 从文本字段更新 Java 变量只能工作一次
- c# - 从父子关系中获取 json 对象树的最佳方法是什么
- android - 如何使用 kivy 从 android 检索联系人
- java - JSP 中的文本框值到同一 JSP 中的 Java scriptlet 代码
- clang - 为什么“-fno-inline”会在clang中带来如此糟糕的性能?
- javascript - 打字稿泛型定义依赖
- android - 使用改造获取数据到 Fragment
- php - JSON by POST 从 Python 到 PHP - 收到“null”返回
- typescript - 在打字稿中导入库时导入variableName vs const variableName
- ios - Xcode 10 NavigationBar 图标未出现在某些设备上