• whenriksen (10/13/2016)


    ...

    SELECT * FROM @T AS t

    WHERE t.c IN ( 1, 2, 10 )

    AND ISNUMERIC(c) = 1--Removing this results in a parsing error

    AND t.c < 3--10 < 3 = false

    ORDER BY t.c;

    ...

    This is not really good example.

    If on a reasonably big table statistics suggest that the condition "t.c < 3" will be significantly more selective that "ISNUMERIC(c) = 1" (which would be considered 50% selective because the use of the function) then the engine will apply the last condition first, before ISNUMERIC, causing conversion error.

    Also, ISNUMERIC is not good for validating integers.

    ISNUMERIC ('.') = 1, but WHERE '.' < 1 causes run-time error.

    To be safe your WHERE clause must look like this:

    SELECT * FROM @T AS t

    WHERE t.c IN ( '1', '2', '10' )

    AND CASE WHEN t.c like '%[^0-9]% -- this checks if any character within the string is not a digit

    THEN NULL ELSE t.c END < 3

    ORDER BY t.c;

    _____________
    Code for TallyGenerator