SQL Server Admin
T-SQL Articles

November 01, 2009

Identify Valid Numeric Expressions in SQL Server




Sometimes the solution to a weird ‘looking’ problem lies in simple SQL Server functions!

I was recently analyzing data of a SQL Server table with a varchar column that contained both numbers and alphabets. The client however now wanted to filter out the rows that contained only numbers in them. Here’s how the requirement was solved

DECLARE @TT table
(
ProductID int,
CodeIdentification varchar
)

-- Create Sample Data
INSERT INTO @TT VALUES ( 101, 'A2')
INSERT INTO @TT VALUES ( 203, '2');
INSERT INTO @TT VALUES ( 305, '2');
INSERT INTO @TT VALUES ( 403, '3');
INSERT INTO @TT VALUES ( 553, 'B3');
INSERT INTO @TT VALUES ( 634, '3');
INSERT INTO @TT VALUES ( 744, '3');
INSERT INTO @TT VALUES ( 838, '4');
SELECT * FROM @TT WHERE IsNumeric(CodeIdentification) = 1

The IsNumeric function determines if the expression passed to it is valid, by returning 1; else it returns 0

The output on running the above query is as shown below:

image


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

 
  Feedback:

comments

1 Response to "Identify Valid Numeric Expressions in SQL Server"
  1. Madhivanan said...
    February 3, 2010 7:07 AM

    Note that isnumeric() is not fully reliable

    Read this
    http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/enhanced-isnumeric-function.aspx

    Madhivanan

 

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