Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

isnumeric issue Expand / Collapse
Author
Message
Posted Monday, June 14, 2010 3:38 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, March 12, 2014 10:09 AM
Points: 189, Visits: 333
When I am using the isnumeric function and passing the character value('2d3') its retrrning it as number. Below is the code.

DECLARE @profile varchar(200)
IF ISNUMERIC('2d3')=1
SET @profile = 'NUMBER'
ELSE
SET @profile = 'CHAR'

SELECT @profile

Can any of you please tell me why its behaving like this. It should return as CHAR, but returning as NUMBER.

Thanks in Advance
Regards,
Naveen
Post #936683
Posted Monday, June 14, 2010 3:45 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, May 30, 2014 6:27 PM
Points: 2,808, Visits: 7,175
becasue '2d3' = 2000 as a valid float value so ISNUMERIC will return 1

SELECT CAST('2d3' AS float)

Post #936686
Posted Wednesday, June 16, 2010 5:37 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, March 12, 2014 10:09 AM
Points: 189, Visits: 333
but when I tried to use the below query

IF ISNUMERIC('$123432345')=1

SET @profile = 'NUMBER'

ELSE

SET @profile = 'CHAR'



SELECT @profile

it is also returning number even though its having $ character.

Please help
Post #938063
Posted Wednesday, June 16, 2010 5:55 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, May 30, 2014 6:27 PM
Points: 2,808, Visits: 7,175
This is because you are passing in a valid money data type so this will return true

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 indicates that expression can be converted to at least one of the numeric types.



I consider ISNUMERIC to be a slightly misleading and possbile dangerous function to use when not fully understood. It is different than a function that will tell you wheter a value contains only numbers and nothing else.

A better what to go about this could be to use PATINDEX, this will only return true where the column contains only Numbers

DECLARE @profile CHAR(10)

IF PATINDEX('%[^0-9]%', '£123432345') = 0
SET @profile = 'NUMBER'

ELSE
SET @profile = 'CHAR'


SELECT @profile

Note: this is not a pefect example as it will dissallow numbers with a decimal place (.) however you can modify the PATINDEX to meet your business requirements
Post #938070
Posted Wednesday, June 16, 2010 6:30 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, March 12, 2014 10:09 AM
Points: 189, Visits: 333
Thanks Steve for the solution provided. It has worked as expected.

Thanks again
Post #938108
Posted Wednesday, June 16, 2010 7:35 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:11 PM
Points: 12,905, Visits: 32,161
Steve i made an ITVF based on your example, but i added a bit more to it;
i was thinking if i allow periods in the data, i need to check for more than one period...so 123.45 would be valud, but 192.168.1.100 would not;
would you agree with that logic?
here's what i put together:
CREATE FUNCTION IsNumeric2(@str varchar(20))
RETURNS int
WITH SCHEMABINDING
AS
BEGIN
declare @results int
SELECT @results = CASE
WHEN (PATINDEX('%[^0-9,.]%', @str) = 0) AND (LEN(@str) - LEN(REPLACE(@str,'.','')) <= 1)
THEN 1
ELSE 0
END
return @results
END --FUNCTION
GO
CREATE FUNCTION IsNumeric3(@str varchar(20))
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN(SELECT CASE
WHEN (PATINDEX('%[^0-9,.]%', @str) = 0) AND (LEN(@str) - LEN(REPLACE(@str,'.','')) <= 1)
THEN 1
ELSE 0
END As boolNumeric
) --END FUNCTION



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #938172
Posted Wednesday, June 16, 2010 7:58 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, May 30, 2014 6:27 PM
Points: 2,808, Visits: 7,175
That looks good and definately improves on the logic, nice work..


I guess in the end it depends on the business rules that the OP are running under would determine what is a 'number' or not.
Post #938190
Posted Thursday, June 17, 2010 2:07 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:22 AM
Points: 1,602, Visits: 5,654
Lowell (6/16/2010)
Steve i made an ITVF based on your example, but i added a bit more to it;
i was thinking if i allow periods in the data, i need to check for more than one period...so 123.45 would be valud, but 192.168.1.100 would not;
would you agree with that logic?


Wouldn't just using ISNUMERIC as well as the PATINDEX work in that case? ISNUMERIC will return false for any number with more than one decimal point, because it can't convert that to a valid number!
Post #938703
Posted Wednesday, December 1, 2010 1:07 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, May 23, 2014 5:59 AM
Points: 329, Visits: 470
Make sure to read this
http://www.sqlservercentral.com/articles/IsNumeric/71512/




Madhivanan

Failing to plan is Planning to fail
Post #1028453
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse