• Lee Dise (10/5/2005)


    T-SQL has an ISNUMERIC function, but for my tastes it is far too tolerant of non-numerics. T-SQL needs an IS-REALLY-TRULY-NUMERIC-HONEST-TO-GOD function.

    I made a function for this and named it "isReallyNumeric" ! 😀

    Here you go:

    /********************************************************************

    Function Purpose:

    This function will return a 1 if the string parameter contains ONLY

    numeric (0-9) digits and will return a 0 in all other cases.

    ~ Created by: Gavin Broughton

    ~ Twitter: http://www.twitter.com/ukgav

    ~ Website: http://www.varchar.co.uk

    ********************************************************************/

    CREATE FUNCTION [dbo].[udf_isReallyNumeric](@inputstring VARCHAR(4000))

    RETURNS int

    BEGIN

    DECLARE @output int

    SELECT @output =

    (CASE WHEN NULLIF(@inputstring,'') /* If string is empty */

    NOT LIKE '%[^0-9]%'/* and LIKE numbers 0-9 (NOT LIKE double negative needed here) */

    THEN 1 ELSE 0 END)/* then return int 1 */

    RETURN @output

    END

    Enjoy! 🙂

    - UKGav

    - Twitter: http://www.twitter.com/ukgav