Click here to monitor SSC

John Magnabosco

SQL Server Development and Data Security

The Hidden Aspects of ISNUMERIC

Published Thursday, June 11, 2009 5:51 AM

I was recently strolling through some of my old blog entries from my pre-Simple-Talk days for inspiration. I ran across an entry on the ISNUMERIC function that I thought that worth re-posting. Enjoy!

SQL Server 2005 Books Online describes the ISNUMERIC function as: "ISNUMERIC returns 1 when the input expression evaluates to a valid integer, floating point number, money or decimal type; otherwise it returns 0. A return value of 1 indicates that expression can be converted to at least one of the numeric types."

To illustrate this, the following statement will return the value of 1 (true):

SELECT ISNUMERIC('486')

While the following statement will return the value of 0 (false):

SELECT ISNUMERIC('ABC')

There are some additional characters that return a positive response to the function that are not regularly considered numeric such as the dollar sign ("$"), the comma (",") and the period ("."). This occurs since they can be converted into other numeric data types. For example, the following statement will return the value of 1 (true) because it can be converted into a money data type:

SELECT ISNUMERIC('$4,860.00')

A strange thing happens when you introduce combinations of numbers and letters. Majority of the letters in the alphabet will return a 0 (false) when examined with the ISNUMERIC function; but when the letter "D" and "E" are introduced a 1 (true) is returned. On the surface, this occurrence may appear to be a bug, or inaccuracy of the function; but it is not.

Let's begin to explain this odd behavior by introducing scientific notation. Scientific notation is a method often used by Engineers, Mathematicians and Scientists to represent very large numbers in a more convenient and easier to read format. Without going into the detailed mathematician-speak regarding this notation please allow the following example to illustrate how scientific notation works: The number of 4,342,000,000,000,000,000,000 when scribed using scientific notation would appear as 4.342 x1021.

Working with scientific notation can be challenging for us Developers who must use a standard QWERTY keyboard to code our applications. Not to mention those who must document such numeric values in spreadsheets and scientific articles; thus, exponential notation was introduced to save the day.

A very simplified description of exponential notation is the representation of x10n with the letter of "E" and followed by the exponent and a character to designate positive and negative numbers. For example: 4.342 x1021 would be represented as 4.342E+21.

When programming those mega-numbers in SQL Server, and many other languages, using exponential notation returns a 32 bit signed single precision floating-point data type (also known simply as "single"). This means that if you exceed the range of -3.40E +38 through 3.40E +38, either truncation or error in conversion will occur. Try the following to illustrate the limitations of the single data type:

SELECT CONVERT(FLOAT(24),'3.40E+39')

The conversion to FLOAT(24), which is also known as a "real" number as well as a single data type, is too small to contain -3.40E +39 and returns the following error:

Arithmetic overflow error converting expression to data type real.

Replacing the letter "E" with "D" will force the scientific notation to return a 64 bit signed double-precision floating-point data type (also known affectionately as "double"). The use of "D" will extend the range returned from -1.79E +308 through 1.79E +308. This is very handy when calculating the expanse of the universe, counting atoms or keeping track of the National debt. Try the following to illustrate the expanse of the double data type:

SELECT CONVERT(FLOAT(53),'1.79D+308')

The conversion to FLOAT(53), which is a double data type, returns the exponential notation that is placed in the argument without error illustrating that the data type is large enough to store the data.
Based upon the definition of scientific notation and exponential notation you might expect the following statement to return 0 (false) because it does not follow the aspect that states that the coefficient (the number that precedes the "E") should be less than 10:

SELECT ISNUMERIC('1001D1')

Since the value can still be converted into a floating point number, the ISNUMERIC function will return 1 (true). This presents a challenge if the value "1001D1" is a serial number instead of scientific notation. If the intent is to evaluate this string to determine whether it includes a nonnumeric character, the consideration of the PATINDEX function would be recommended.

With the introduction of the regular expression "[^0-9]" as an argument to PATINDEX, the location of the non-numeric character is returned. If the value of 0 is returned, the string does not include any non-numeric values. An example of this function's syntax would be:

PATINDEX('%[^0-9]%', '1001D1')

Keeping this information in mind in regard to the tricky ISNUMERIC function and how to utilize the PATINDEX function as an alternate will save you so much time that you might just need to use scientific notation to represent the minutes available in your day for other activities such as sleep.

by Johnm

Comments

 

JoshRoss said:

What I do not like about SQLs handling of the 64bit double is the following behavior:  

-- this works as expected
select 123e10

-- this does not, it parses as select 123 as [d10]
select 123d10
June 11, 2009 5:57 PM
 

Dave Turpin, Database Professional » Letters and Money in ISNUMERIC may return 1 said:

November 8, 2009 5:31 PM
 

C2500 Blower, C2500 Suburban Aftermarket Chevrolet Camaro Repair Shops said:

May 20, 2010 6:27 PM
 

Find Used Toyota 4runner Dealers, Toyota 4runner Gibson said:

May 20, 2010 6:49 PM
 

Blackwood Bulb Hid Conversion Lincoln Mkx, Lincoln Blackwood Car Parts Fog Light said:

May 21, 2010 3:45 AM
 

J30 Ad Browse, J30 Auto Parts Infiniti Fx45 said:

May 21, 2010 5:41 PM
 

G2500 Replacement Assembly Wiring Harness, G2500 Headlight Buy Used Chevrolet Express said:

May 21, 2010 6:21 PM
 

Avi 300ce, 300ce Free Need said:

May 21, 2010 10:02 PM
 

Sl1 Motorcycle, 1998 Saturn Sl1 Problems said:

May 22, 2010 12:27 AM
 

Removal 2003 Buick Lesabre, Century Dash Removal Buick Lesabre said:

May 22, 2010 1:07 AM
 

1990 Mitsubishi Mighty Max Body Parts Expo Expo, Exposition Mitsubishi Montero Sport said:

May 22, 2010 1:07 AM
 

Chevrolet G10 Sale Classified Guide, Used Image Stabilization Point And Shoot Canon Powershot G10 said:

May 22, 2010 4:36 AM
 

Replacement Parts Nissan Frontier Brake Pads, Model Nissan Frontier Grille Guard said:

May 22, 2010 6:19 AM
 

Discount Mercedes Benz 240d Oxygen Sensor Catalytic Converter, 1983 Mercedes 240d Hp said:

May 22, 2010 7:56 AM
 

Optima Yellow Top Charger, Kia Optima Battery Led said:

May 22, 2010 8:06 AM
 

Farm Town Spreadsheet, 2006 Lincoln Town Car Msrp said:

May 22, 2010 11:00 AM
 

Dodge B200 Bulb Delco Bendix, B2000 Headlight Mazda Rx7 said:

May 22, 2010 11:01 AM
 

Milan Floor, Mercedes 380slc Cars For Sale Milan Brown 450se said:

May 22, 2010 11:20 AM
 

S40 Bumper Replacement Volvo V50, Volvo V50 Safety said:

May 22, 2010 12:15 PM
 

265 Part 1980 Volvo Xc70, 265wt Bulb Tomtom Xl said:

May 22, 2010 12:28 PM
 

E 450 Diesel Shopping, F 350 Super Duty Replacement 2008 Ford F350 said:

May 22, 2010 4:43 PM
 

Mirage Casino Sold Phoenix Real Estate, 1997 Mitsubishi Mirage Transmission said:

May 22, 2010 5:05 PM
 

Bmw 323is Part Brake Pad Wear, 323is Tube Fit said:

May 22, 2010 5:08 PM
 

250sl Second Hand Auction, Headlight 250sl Cls63 Amg Mercedes Benz 300sl said:

May 22, 2010 6:05 PM
 

Night Vision Canada, C32a Panasonic Parts Division said:

May 22, 2010 7:35 PM
 

Discount Century 21, Century Coolant Oldsmobile - 282.akemet.com said:

May 22, 2010 10:35 PM
 

Mercedes C300 Aftermarket Parts, C30 Pickup Full Wholesale Price - 322.mfbattle.com said:

May 22, 2010 11:33 PM
 

Used Dodge Ramcharger Parts Wiring Harness, Dell Replacement Battery Charger - 421.animejin.com said:

May 23, 2010 2:57 AM
 

V1500 Suburban Brake Caliper Truck Parts Brakes, Dodge B1500 Air Filter - 177.tijuanareader.com said:

May 23, 2010 3:11 AM
 

Full Moon Nearest Autumnal Equinox, Campmor Equinox Silnylon Poncho Tarp - 452.computeronlinebingo.com said:

May 23, 2010 5:59 AM
 

Adobe Premiere Direct Stop, Premier Artwork - 350.myipgirl.com said:

May 23, 2010 7:33 AM
 

Caballero Transmission Around, Headlight Chrome Gmc Caballero - 323.animejin.com said:

May 23, 2010 1:35 PM
 

Aerio Custom Wheel Covers Universal, Sidekick Radiator Suzuki Aerio Silla - 355.binggreen.com said:

May 25, 2010 8:22 PM
 

Concessionnaire Mercedes Benz Luxembourg, Continental Tube 12 Volt Air Conditioning - 382.1fh.org said:

May 25, 2010 8:29 PM
You need to sign in to comment on this blog

About Johnm

John Magnabosco manages the Data Services Group at one of the fastest growing companies in the United States. He is also a Co-Founder of the Indianapolis Professional Association for SQL Server (IndyPASS), Co-Founder of IndyTechFest, the author of the book titled "Protecting SQL Server Data" and contributing author of "SQL Server MVP Deep Dives Volume 2".
<June 2009>
SuMoTuWeThFrSa
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Converting String Data to XML and XML to String Data
 We all appreciate that, in general, XML documents or fragments are held in strings as text markup. In... Read more...

Geek of the Week: Don Syme
 With the arrival of F# 3.0 Microsoft announced a wide range of improvements such as type providers that... Read more...

How to Document and Configure SQL Server Instance Settings
 Occasionally, when you install identical databases on two different SQL Server instances, they will... Read more...

What's the Point of Using VARCHAR(n) Anymore?
 The arrival of the (MAX) data types in SQL Server 2005 were one of the most popular feature for the... Read more...