I have this possible values in a column
1
65
5 excellent
54
-1
-
.
If I use isnumeric with the last example I get 1, but when I try to convert to number I got an error. I want to use a try-catch in a function but I can't, how can I deal with this?
By the way, an even worse example is '-.', which isnumeric() considers to be valid.
My advice is to look for at least one digit in the value as well. Yucky, but:
isnumeric(val) and val like '%[0-9]%'
Note that isnumeric() also considers something in exponential notation to be valid. So '8e4' will test as positive. This may not be an issue for you, because it will convert to a valid value. Such matches have caused a problem for me in the past, so I tend to use something like:
val not like '%[^0-9.]%' and val not like '%.%.%' and val like '%[0-9]%'
That is, it only has decimal points and digits. And, it doesn't have two decimal points. But, it only works for positive values.
I think you are looking for something like this:
select case isnumeric('a') when 1 then convert(int,'a') else null end
Could you explain your goal? Something like this could be useful:
SELECT CASE ISNUMERIC(col)
WHEN 1 THEN CAST(col as float) -- or int, decimal, etc.
ELSE NULL -- Or 0, -9999, or whatever value you want to use as "exception" value
END
The TRY…CATCH construct cannot be used in a user-defined function.
http://msdn.microsoft.com/en-us/library/ms175976%28v=SQL.105%29.aspx
If you are working on SQL Server 2012, I recommend using TRY_CONVERT:
select TRY_CONVERT(int, '.') returns NULL instead of an error.
Related
In a field I want to keep those where it is only a number between 100000 and 999999
This field has a mixture of numeric, alphanumeric, alphabet characters.
This function will keep if there is a number in a field e.g. keep abc123 where I would want that removed
where some_column NOT LIKE '%[^0-9]%'
Additional - Celonis Equivalent ?
Gordon has kindly provided SQL Server coding to do this exactly as intended.
I was hoping this would work in Celonis but the try_convert function is not supported. Is there another method?
where try_convert(int, some_column) between 100000 and 999999
First of all it is good to know that Celonis uses Vertica SQL for its event collection. Vertica has some specific functions, that you can find at https://www.vertica.com/docs/.
That said, I'm not sure if I understand your question well, but I think that the REGEXP_ILIKE is something you could use, where '[0-9]' could be the expression you are looking for to check if your field contains a numeric value.
If you want to extract the number itself (for instance to check if it is between 100000 and 999999, use then REGEXP_SUBSTR.
Your code will be something like:
WHERE REGEXP_SUBSTR(some_column, [0-9]) BETWEEN 100000 AND 999999
Your syntax suggests you are using SQL Server. If so, try:
where try_convert(int, some_column) between 100000 and 999999
I am trying to compare two string using Sql query. for e.g In table A i have A123.45 and in table B i have A12345. this two string are same if i ignore decimal point so as a output i would want table A's value.
First, to avoid the XY problem, it's a little unclear to me why you'd want to do this in the first place - I'm not sure exactly why 123.45 should be equal to 12345. Definitely something to think about.
With that said, if you insist, you can do something like the following:
select case when replace(cast(floatingPointNumber as varchar(50)), '.', '') = cast(yourInteger as varchar(50)) then 1 else 0 end
from YourTable
Obviously, floatingPointNumber is a float and yourInteger is an integer.
I'm not sure what platform you're using since you didn't tag it but I wrote/tested this in SQL Server. You can do something similar in Oracle/MySQL if that's what you're using.
Basically, what this is doing is casting both the floating point number and the integer to strings, removing the decimal from the floating point number, and comparing them. If they're equal, it returns 1; otherwise it returns 0.
I am trying to check if a string contains only valid number in the following format
123.456
123
.356
But it should reject anything that contains non-numbers including double dots. Here are some invalid formats
d123.456
123d
12d3
d.1256
12d.456
12.d12
12.d45d
12.45.56
I have done the following
SELECT CASE WHEN '123.00' NOT LIKE '%[^0-9.]%' THEN 'Valid' ELSE 'Invalid' END
When seems to work except for the case where there is more than one dot in the string.
How can I tweak the regular expression to only allow one dot otherwise return 'Invalid'?
I would suggest try_convert():
select (case when try_convert(col, float) is not null then 'valid' else 'invalid' end)
The one possible downside is exponential format; 1e6 is a valid number for instance.
An alternative is the where approach; you just need more complete logic:
select (case when col like '%[^0-9.]%' then 'invalid'
when col like '%.%.%' then 'invalid'
else 'valid'
end)
There's a sql server built in function:
Select CASE WHEN isnumeric([fieldname]) THEN 'Valid' ELSE 'Invalid" END
If you're not tied to regular expressions, SQL Server has an ISNUMERIC function you can use for this.
ISNUMERIC returns 1 when the input expression evaluates to a valid numeric data type; otherwise it returns 0.
TRY_PARSE will let you compare the input value to any/all numeric datatypes you decide to allow -- for example:
SELECT
TRY_PARSE('123.456' as int) as [int],
TRY_PARSE('123.0' as float) as [float],
TRY_PARSE('d123.456' as int) as [int],
TRY_PARSE('d123.456' as float) as [float]
FWIW -- ISNUMERIC is often suggested, and is certainly the best-sounding function name :-) -- but doesn't work the way most folks seem to expect. (It allows math and currency symbols, etc.)
Alternative solution for NOT LIKE clause is:
where REGEXP_LIKE(column, '^[[:digit:]]+$')
I need to checking a column where numeric or not in SQL Server 2012.
This my case code.
CASE
WHEN ISNUMERIC(CUST_TELE) = 1
THEN CUST_TELE
ELSE NULL
END AS CUSTOMER_CONTACT_NO
But when the '78603D99' value is reached, it returns 1 which means SQL Server considered this string as numeric.
Why is that?
How to avoid this kind of issues?
Unfortunately, the ISNUMERIC() function in SQL Server has many quirks. It's not exactly buggy, but it rarely does what people expect it to when they first use it.
However, since you're using SQL Server 2012 you can use the TRY_PARSE() function which will do what you want.
This returns NULL:
SELECT TRY_PARSE('7860D399' AS int)
This returns 7860399
SELECT TRY_PARSE('7860399' AS int)
https://msdn.microsoft.com/en-us/library/hh213126.aspx
Obviously, this works for datatypes other than INT as well. You say you want to check that a value is numeric, but I think you mean INT.
Although try_convert() or try_parse() works for a built-in type, it might not do exactly what you want. For instance, it might allow decimal points, negative signs, and limit the length of digits.
Also, isnumeric() is going to recognize negative numbers, decimals, and exponential notation.
If you want to test a string only for digits, then you can use not like logic:
(CASE WHEN CUST_TELE NOT LIKE '%[^0-9]%'
THEN CUST_TELE
END) AS CUSTOMER_CONTACT_NO
This simply says that CUST_TELE contains no characters that are not digits.
Nothing substantive to add but a couple warnings.
1) ISNUMERIC() won't catch blanks but they will break numeric conversions.
2) If there is a single non-numeric character in the field and you use REPLACE to get rid of it you still need to handle the blank (usually with a CASE statement).
For instance if the field contains a single '-' character and you use this:
cast(REPLACE(myField, '-', '') as decimal(20,4)) myNumField
it will fail and you'll need to use something like this:
CASE WHEN myField IN ('','-') THEN NULL ELSE cast(REPLACE(myField, '-', '') as decimal(20,4)) END myNumField
Is there an easy way to get if string is an integer number (consists only of digits) in MS SQL 2005?
Thank you for your help.
The function ISNUMERIC returns whether a string is numeric, but will return true for non-integers.
So you could use:
WHERE ISNUMERIC(str) AND str NOT LIKE '%.%' AND str NOT LIKE '%e%' AND str NOT LIKE '%-%'
Even though the original poster was referring to SQL 2005, I found in 2008 r2 a straight where isnumeric(string) resulted in an error 4145 non-boolean type. To resolve this use:where isnumeric(string) = 1
You could use the LIKE operator:
WHERE str NOT LIKE '%[^0-9]%'
See this:
CREATE Function dbo.IsInteger(#Value VarChar(18))
Returns Bit
As
Begin
Return IsNull(
(Select Case When CharIndex('.', #Value) > 0
Then Case When Convert(int, ParseName(#Value, 1)) <> 0
Then 0
Else 1
End
Else 1
End
Where IsNumeric(#Value + 'e0') = 1), 0)
End
It is a little tricky to guarantee that a value will conform to a 4 byte integer.
Since you are using 2005 - One way is to try to convert the value within a try/catch block. That would be the best way to insure that it is actually an int. Of course you need to handle the cases when it does not in the catch block according to your requirements.
Another way to just test for only "digits" is this:
where strVal not like '%[^0-9]%'
That will miss -25. as well as allow '99999999999999999999'
So you may need to include additional criteria with this method.
Using (CAST(PATINDEX('%[^0-9]%', value) as BIT)) handles all the characters
Standard T-SQL function ISNUMERIC ( expression )
Determines whether an expression is a valid numeric type.