Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

ISNUMERIC Trouble

From Wiki

Jump to: navigation, search

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

  1. DECLARE @S VARCHAR(50)
  2.     SET @S = CHAR(9) --- @S NOW CONTAINS THE TAB CHARACTER
  3.     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

  1. CREATE TABLE #foo (VALUE VARCHAR(20))
  2.     INSERT INTO #foo
  3.     SELECT '1' UNION ALL
  4.     SELECT '3' UNION ALL
  5.     SELECT 'B' UNION ALL
  6.     SELECT '2' UNION ALL
  7.     SELECT '33.331' UNION ALL
  8.     SELECT 'adad1' UNION ALL
  9.     SELECT '1d2' UNION ALL
  10.     SELECT '^' UNION ALL
  11.     SELECT '17777.999'


returns ^

  1. SELECT * FROM #foo
  2.     WHERE VALUE NOT LIKE '%[a-z]%'


returns 1d2

  1. SELECT * FROM #foo
  2.     WHERE ISNUMERIC(VALUE) = 1


returns correct result

  1. SELECT * FROM #foo
  2.     WHERE VALUE NOT LIKE '%[a-z]%'
  3.     AND ISNUMERIC(VALUE) = 1


Contributed by: --SQLDenis 03:17, 31 May 2008 (GMT)

Part of SQL Server Programming Hacks

Section Pitfalls

192 Rating: 2.7/5 (33 votes cast)