代码之家  ›  专栏  ›  技术社区  ›  Abs

将nvarchar转换为int时转换失败

  •  3
  • Abs  · 技术社区  · 15 年前

    Conversion failed when converting the nvarchar value '16:00' to data type int.
    

    select id, case(isnumeric([other08])) when 1 then [other08] else 0 end
    from CER where sourcecode like 'ANE%' --and other08 > 720
    

    当我取消最后一部分的注释时,它失败了。

    我试图让所有的数字都大于720,但我不能做Comaprison。铸造和转换时也会失败。

    谢谢你的帮助

    2 回复  |  直到 15 年前
        1
  •  7
  •   Winston Smith    15 年前

    您还需要在WHERE子句中执行检查和转换:

    SELECT 
           id, 
           CASE WHEN isnumeric([other08]) = 1 THEN CAST([other08] AS INT) ELSE 0 END
    FROM   CER 
    WHERE  sourcecode LIKE 'ANE%' 
    AND CASE WHEN isnumeric([other08]) = 1 THEN CAST([other08] AS INT) ELSE 0 END > 720
    
        2
  •  2
  •   dsolimano    15 年前

    select id, case(isnumeric([other08])) when 1 then [other08] else 0 end
    from CER 
    where sourcecode like 'ANE%' and ISNUMERIC(other08) = 1 and other08 > 720
    

    正如@abs指出的那样,上述方法不起作用。但是,我们可以使用CTE计算要过滤的可靠字段:

    WITH Data AS (
      select id
        , case WHEN isnumeric([other08]) THEN CAST([other08] AS int) else 0 end AS FilteredOther08
        , CER.* 
      from CER 
      where sourcecode like 'ANE%'
    )
    SELECT *
    FROM Data
    WHERE [FilteredOther08] > 720