Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 31st, 2003, 02:42 AM
Authorized User
 
Join Date: Jun 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default ISNUMERIC function

Hi,

I am trying to check all the values in my table are numeric and I am using ISNUMERIC function for that. But, for the following statements
ISNUMERIC(',') or ISNUMERIC('.') is returning true and I am getting the conversion errors at later stage.

Please suggest me any Solution for this (or) any alternate Ideas? I am using SQL 2000 with SP3.
 
Old November 3rd, 2003, 09:29 AM
Authorized User
 
Join Date: Oct 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SubodhKumar Send a message via Yahoo to SubodhKumar
Default

Hi Ram,
Actually characters like dollar($),comma(,) and dot(.) is used to represent numberic data.
   E.g.
       $5000.00
       5000.75
       56,775.00
thats why these character is treated as numeric data.

To avoid this you can write a query in different way like.......

Suppose we have a table
create table temps
(
fld varchar(100)
)

and data of table like
19
29
23
subodh
.
,
.,
=
$

When you run the query:: select fld,isnumeric(fld) as 'Isnumeric' from temps
it will return:
fld Isnumeric
19 1
29 1
23 1
subodh 0
. 1
, 1
., 1
= 0
$ 1

Obviously the output is not desired.To get the desired result we can write our query like:-
select fld,case
        when fld like '%.%' then 0
        when fld like '%,%' then 0
        when fld like '%$%' then 0
        else isnumeric(fld)
          end as 'IsNumeric'
from temps

it will give correct output:-
fld IsNumeric
19 1
29 1
23 1
subodh 0
. 0
, 0
., 0
= 0
$ 0

On the above concept, Now I hope you can solve your problem.



Quote:
quote:Originally posted by ram2098
 Hi,

I am trying to check all the values in my table are numeric and I am using ISNUMERIC function for that. But, for the following statements
ISNUMERIC(',') or ISNUMERIC('.') is returning true and I am getting the conversion errors at later stage.

Please suggest me any Solution for this (or) any alternate Ideas? I am using SQL 2000 with SP3.
Enjoy!
Subodh Kumar
Phoneytunes.com
 
Old November 3rd, 2003, 03:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by SubodhKumar


<snippage>

Actually characters like dollar($),comma(,) and dot(.) is used to represent numberic data.
E.g.
     $5000.00
     5000.75
     56,775.00
thats why these character is treated as numeric data.

Obviously the output is not desired.To get the desired result we can write our query like:-
select fld,case
        when fld like '%.%' then 0
        when fld like '%,%' then 0
        when fld like '%$%' then 0
        else isnumeric(fld)
         end as 'IsNumeric'
from temps
it will give correct output :-
Nope.

Your WHERE criteria would reject a valid numeric string like 19.25 as non numeric.

This is actually an interesting issue. From BOL:

"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 guarantees that expression can be converted to one of these numeric types".

Apparently, the rule is whether the CAST function would throw an error attempting to convert the string to one of the numeric types. Many seemingly invalid numeric strings in fact result in a ISNUMERIC value of 1. For example, these strings all result in a 1:

.
,
,.
.,
$.
$,.
$,9.
9,9. --!!! this will CAST to money as 99.0000
etc. etc.

because you can CAST each of these strings to a money type and you get a "reasonable" value, e.g. CAST('.' as money) results in a value of .0000.

What's weird is that if you attempt to CAST the same string to any other type (like decimal) you'll get a conversion error. What's even worse, the string '1E10' will return a 1, but a CAST to money will result in a conversion error. Of course, that string will successfully CAST to FLOAT.

Unfortunately, it seems ISNUMERIC doesn't tell you which numeric type it can successfully CAST to, only that it can make some successful CAST...

I think this is a flaw in the definition and behavior of ISNUMERIC, and I have no good solution, short of writing an ugly user defined function which makes use of the LIKE operator and other string operators to parse your string into what you would define as a valid numeric string, and not what SQL Server ISNUMERIC does...

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
IsNumeric Bug thicks VB.NET 1 June 26th, 2007 06:55 PM
IsNumeric Query rsm42 ASP.NET 1.0 and 1.1 Basics 3 June 13th, 2007 02:40 PM
IsNumeric ? Berggreen C# 1 June 21st, 2006 07:05 PM
How to call javascript function from VB function vinod_yadav1919 VB How-To 0 February 13th, 2006 06:03 AM
retreive function/Line from macro or function? MikoMax J2EE 0 April 1st, 2004 04:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.