Azure数据仓库-在Float数据类型列中修复bigint值

时间:2019-01-17 17:16:09

标签: sql sql-server tsql azure-sql-database

我有一个float数据类型的列。包含bigint值的错误数据。值为-4.66606E+22

我尝试了以下逻辑,但无法正常工作。

  1. 使用case语句检查列是否在float最小和最大范围之间,如果不是,则将其更改为0。

  2. 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

2 个答案:

答案 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-30802.23E-3081.79E+308

我们可以在这里看到: enter image description here

参考:

  1. float and real
  2. int, bigint, smallint, and tinyint

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 precedenceFloat

执行摘要:“存在一个错误数据,其值为bigint。值为-4.66606E+22。”那是真的坏数据。比较BigIntFloat的值是不精确的活动,很可能会导致失望。