Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
ISNUMERIC Trouble
From Wiki
If you use the ISNUMERIC function to determine if a value is numeric, you might be in for a surprise. Run the 3 lines of code below and you will see what I mean
- DECLARE @S VARCHAR(50)
- SET @S = CHAR(9) --- @S NOW CONTAINS THE TAB CHARACTER
- SELECT ISNUMERIC(@S), ISNUMERIC(CHAR(9)),ISNUMERIC('1D2'),ISNUMERIC('D')
As you can see, TAB is returned as numeric as well the value 1D2.
A better way to test for this would be with LIKE and %[a-z]%
If you run the example below you will see that the select statement with the ISNUMERIC function or LIKE returns one row more than the statement with LIKE and ISNUMERIC combined
- CREATE TABLE #foo (VALUE VARCHAR(20))
- INSERT INTO #foo
- SELECT '1' UNION ALL
- SELECT '3' UNION ALL
- SELECT 'B' UNION ALL
- SELECT '2' UNION ALL
- SELECT '33.331' UNION ALL
- SELECT 'adad1' UNION ALL
- SELECT '1d2' UNION ALL
- SELECT '^' UNION ALL
- SELECT '17777.999'
returns ^
- SELECT * FROM #foo
- WHERE VALUE NOT LIKE '%[a-z]%'
returns 1d2
- SELECT * FROM #foo
- WHERE ISNUMERIC(VALUE) = 1
returns correct result
- SELECT * FROM #foo
- WHERE VALUE NOT LIKE '%[a-z]%'
- AND ISNUMERIC(VALUE) = 1
Contributed by: --SQLDenis 03:17, 31 May 2008 (GMT)
Part of SQL Server Programming Hacks
Section Pitfalls
LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.