|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, November 22, 2012 2:43 AM
Points: 177,
Visits: 268
|
|
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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 7:42 AM
Points: 2,802,
Visits: 7,103
|
|
becasue '2d3' = 2000 as a valid float value so ISNUMERIC will return 1
SELECT CAST('2d3' AS float)
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, November 22, 2012 2:43 AM
Points: 177,
Visits: 268
|
|
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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 7:42 AM
Points: 2,802,
Visits: 7,103
|
|
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
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, November 22, 2012 2:43 AM
Points: 177,
Visits: 268
|
|
Thanks Steve for the solution provided. It has worked as expected.
Thanks again
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Yesterday @ 3:24 PM
Points: 11,605,
Visits: 27,649
|
|
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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 7:42 AM
Points: 2,802,
Visits: 7,103
|
|
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.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 4:56 AM
Points: 1,256,
Visits: 4,253
|
|
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!
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, September 12, 2012 5:17 AM
Points: 329,
Visits: 461
|
|
|
|
|