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

SQL Server中ISinteger的最佳等价项

  •  29
  • Mayo  · 技术社区  · 15 年前

    在SQL Server(2000/2005/2008)中,确定字段值是否为整数的最佳方法是什么?

    对于不太可能转换为整数的各种格式,isnumeric返回true。例如“15000”和“15.1”。

    您可以使用like语句,但这似乎只适用于具有预定义位数的字段…

    select * where zipcode like '[0-9][0-9][0-9][0-9][0-9]'
    

    我可以编写一个用户定义的函数,尝试在try/catch块中将varchar参数转换为int,但我正在与社区联系,看看是否有人遇到了实现此目标的任何成功方法,最好是可以在sql st的where子句中使用的方法。不创建其他对象的属性。

    11 回复  |  直到 15 年前
        1
  •  35
  •   AdaTheDev    15 年前

    1方法是

    zipcode NOT LIKE '%[^0-9]%'
    

    双底片,一定要喜欢!

        2
  •  52
  •   gbn    15 年前

    处理负片的延迟输入

    ISNUMERIC(zipcode + '.0e0') --integer
    ISNUMERIC(zipcode + 'e0')  --decimal
    

    为了更多的了解 this

        3
  •  6
  •   OMG Ponies    15 年前

    如果SQL Server 2005+,我会 enable CLR and create the function to support regexes . 对于SQL Server 2000, see this article for creating a UDF to do the same thing .

    然后我用正则表达式: ^\d{5}$

        4
  •  4
  •   fancyPants    12 年前

    此表达式为整数值提供1,否则为0

    floor((floor(abs(zipcode)))/abs(zipcode))
    
        5
  •  0
  •   Community CDub    7 年前

    我在另一个stacko问题上找到了完美的答案。
    这也证明了你 不能 使用“.0e0”,就像一个用户在这里建议的那样。
    它没有CLR或非标量函数。
    请检查一下: https://stackoverflow.com/a/10645764/555798

        6
  •  0
  •   cjbarth    12 年前

    为什么不直接用下面的呢?我找不到任何失败的案例。

    • 1 =整数
    • 0=非整数
    • 空=非数字
    DECLARE @TestValue nvarchar(MAX)
    SET @TestValue = '1.04343234e5'
    
    SELECT CASE WHEN ISNUMERIC(@TestValue) = 1
            THEN CASE WHEN ROUND(@TestValue,0,1) = @TestValue
                THEN 1
                ELSE 0
                END
            ELSE null
            END AS Analysis
    
        7
  •  0
  •   jessieloo    9 年前

    在转到sql 2008之后,我正在努力处理isnumeric('\8')返回true,但在强制转换为整数时抛出错误。显然正斜杠是日元或韩元的有效货币 http://www.louiebao.net/blog/200910/isnumeric/ )

    我的解决办法是

    case when ISNUMERIC(@str) > 0 and not rtrim(@str) LIKE '[^0-9]%'  and not rtrim(@str) LIKE '%[^0-9]' and not rtrim(@str) LIKE '[^0-9]%' then rtrim(@str) else null end
    
        8
  •  0
  •   Gopakumar N.Kurup    8 年前

    看看下面的代码是否有用。 在下面的值中,只有921474836741234567符合 整数。我们可以创建这个函数并使用它。

    CREATE TABLE MY_TABLE(MY_FIELD VARCHAR(50))
    INSERT INTO MY_TABLE
    VALUES('9.123'),('1234567'),('9'),('2147483647'),('2147483647.01'),('2147483648'), ('2147483648ABCD'),('214,7483,648')
    
    SELECT *
    FROM MY_TABLE
    WHERE CHARINDEX('.',MY_FIELD) = 0 AND CHARINDEX(',',MY_FIELD) = 0       
    AND ISNUMERIC(MY_FIELD) = 1 AND CONVERT(FLOAT,MY_FIELD) / 2147483647 <= 1
    DROP TABLE MY_TABLE
    
        9
  •  0
  •   Todd181    8 年前

    我是用一个案例陈述的: cast(当quantity/[[of days]=cast(quantity/[[of days]作为int)然后abs(quantity/[[of days])否则0以int结束)

        10
  •  0
  •   vibs2006    7 年前

    要测试输入值是否为整数,我们可以使用 sql_variant_属性 SQL Server的功能。

    下面的sql脚本将接受输入并测试数据类型是否为整数

    declare @convertedTempValue bigint, @inputValue nvarchar(255) = '1' --Change '1' to any input value
    set @convertedTempValue = TRY_PARSE(@inputValue as bigint) --we trying to convert to bigint
    declare @var3 nvarchar(255) = cast (SQL_VARIANT_PROPERTY(@convertedTempValue,'BaseType') as nvarchar(255)) --we using SQL_VARIANT_PROPERTY to find out datatype
    if ( @var3 like '%int%')
        begin
        print 'value is integer'
        end
    else
        begin
        print 'value is non integer'
        end
    go
    
        11
  •  -2
  •   HLGEM    15 年前

    也许您应该只在整数数据类型中存储整数数据。