T-sql - determine if value is integer

后端 未结 20 2183
情深已故
情深已故 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:34

    Use TRY_CONVERT which is an SQL alternative to TryParse in .NET. IsNumeric() isn’t aware that empty strings are counted as (integer)zero, and that some perfectly valid money symbols, by themselves, are not converted to (money)zero. reference

    SELECT @MY_VAR = CASE WHEN TRY_CONVERT(INT,MY_FIELD) IS NOT NULL THEN MY_FIELD
                     ELSE 0
                     END
    FROM MY_TABLE
    WHERE MY_OTHER_FIELD = 'MY_FILTER'
    

提交回复
热议问题