April 24, 2008

How to check if a string contains numbers

There are number of ways in SQL Server to check if a string has numbers in it. In one of the previous articles, we saw how to Extract Numbers from a String using Sql Server 2005

I recently got a comment on that article from a sqlservercurry.com visitor who asked me to find out if a string has numbers, using PatIndex. Here's how to do so using PatIndex :

DECLARE @str1 varchar(15)
SET @str1 = 'abcde99jtk'

-- Check if PatIndex returns a value > 0. If yes, the string has numbers
-- If the value is 0, the string does not have numbers
IF PATINDEX('%[0-9]%',@str1) > 0
PRINT 'YES, The string has numbers'
PRINT 'NO, The string does not have numbers'

Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter

About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal



2 Responses to "How to check if a string contains numbers"
  1. Anonymous said...
    July 21, 2008 at 12:49 PM

    Awesome, thanks for the post! solved it!

  2. amirali1982 said...
    February 6, 2009 at 10:48 PM

    thanks a lot, this query helped me with a related problem...


Copyright © 2009-2016 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions