T-sql - determine if value is integer

后端 未结 20 2214
情深已故
情深已故 2020-11-30 12:11

I want to determine if a value is integer (like TryParse in .NET). Unfortunatelly ISNUMERIC does not fit me because I want to parse only integers a

20条回答
  •  不知归路
    2020-11-30 12:48

    I have a feeling doing it this way is the work of satan, but as an alternative:

    How about a TRY - CATCH?

    DECLARE @Converted as INT
    DECLARE @IsNumeric BIT
    
    BEGIN TRY
        SET @Converted = cast(@ValueToCheck as int)
        SET @IsNumeric=1
    END TRY
    BEGIN CATCH
        SET @IsNumeric=0
    END CATCH
    
    select IIF(@IsNumeric=1,'Integer','Not integer') as IsInteger
    

    This works, though only in SQL Server 2008 and up.

提交回复
热议问题