Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


IsNumeric


IsNumeric

Author
Message
balamurugan.ganesan
balamurugan.ganesan
SSC Veteran
SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)

Group: General Forum Members
Points: 221 Visits: 110
Hi,

May be u all know this early but i wanted to post this.
I came across a phenomenon with the isnumeric function. Whenever we use

select isnumeric('1111d1')

the result gives as 1 instead of zero this happens for

select isnumeric('111d11')
select isnumeric('11d111')

and not for select isnumeric('11111d') or select isnumeric('1d1111') or select isnumeric('d11111').

The same happens when 'e' is used. for all other alphabets it works fine.

Thanks & Regards,
Balamurugan G
Jez-448386
Jez-448386
SSC Veteran
SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)

Group: General Forum Members
Points: 278 Visits: 1126
isNumeric returns True (1) when the string can be converted into a number.

'1111D1' can be converted into a number 1111 - D for decimal
'11E2' can also be converted into a number 1100 - E for exponent (power of 10).

If you need to check that the string only contains numerics then you will need a different piece of code e.g patindex


Jez
Scalability Doug
Scalability Doug
Say Hey Kid
Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)

Group: General Forum Members
Points: 665 Visits: 6111
The same holds true for monetary values , '$1,234'.


DAB
Gonzalo-603688
Gonzalo-603688
Valued Member
Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)

Group: General Forum Members
Points: 70 Visits: 31
I think you can use something like this:

CREATE FUNCTION PureNumeric
(
   @STRING VARCHAR(255)
)
RETURNS BIT
AS
BEGIN
IF ISNUMERIC(@STRING) = 1
BEGIN
   IF (CHARINDEX('D', @STRING) <> 0) OR (CHARINDEX('E', @STRING) <> 0) OR ( CHARINDEX('$', @STRING) <> 0)
      RETURN 0
   ELSE
      RETURN 1
END
RETURN 0
END



I did this for you as an idea!

Cheers,
G
Kent Waldrop
Kent Waldrop
SSC-Addicted
SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)

Group: General Forum Members
Points: 480 Visits: 467
Another potential idea is something like:

declare @test table(tString varchar(12))
insert into @test
select null union all
select '' union all
select '$1,234' union all
select '1111D1' union all
select '11E2' union all
select '-1.2' union all -- Valid numeric
select '+0.5' union all -- Valid numeric
select '+1.3.2' union all
select '215' union all -- Valid numeric
select '215+'

-- --------------------------------------------------------------------------
-- (1) '[-.+0-9' provides for a leading sign or decimal place
-- (2) '[.0-9]' provides for digits or a decimal point in the string
-- (3) The length comparison makes sure that there is only 1 decimal point
-- --------------------------------------------------------------------------
select
tstring,
isNumeric(tString) as [isNumeric],
case when len(tString) - len(replace(tString,'.','')) <= 1
and tString like '[-.+0-9]' + replicate('[.0-9]', len(tstring) - 1)
then 1 else 0 end
as isTested
from @test

/* -------- Sample Output: --------
tstring isNumeric isTested
------------ ----------- -----------
NULL 0 0
0 0
$1,234 1 0
1111D1 1 0
11E2 1 0
-1.2 1 1
+0.5 1 1
+1.3.2 0 0
215 1 1
215+ 0 0
*/
SwePeso
SwePeso
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2465 Visits: 3431
If only digits are allowed, try this

SELECT   *
FROM   Table1
WHERE   Col1 NOT LIKE '%[^0-9]%'


N 56°04'39.16"
E 12°55'05.25"
@STLSQLGuy
@STLSQLGuy
SSC Journeyman
SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)

Group: General Forum Members
Points: 92 Visits: 209
This is an example using the PatIndex. I believe it's just one of many options but will work. Just add this WHERE Clause to your statement to return only items that are truely simple numbers


where PatIndex('%[^0-9]%',(RTRIM(LTRIM(YourColName)))) = 0



Thanks,

Eddie H


thanks, ERH

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search