You have a char column that can has (logically) only two types of data for each record: digits or text. You want to make a query using comparison operators (Greater than, Less than..) to fetch only records contains digits.
You can use the isnumeric() function, but the following problems occur:
SELECT ISNUMERIC('1212') --> 1: exactly
SELECT ISNUMERIC('12C2') --> 0: exactly
SELECT ISNUMERIC('10E0') --> 1: wrong
SELECT ISNUMERIC('1,100') --> 1: wrong
SELECT ISNUMERIC('+') --> 1: wrong
SELECT ISNUMERIC('-') --> 1: wrong
SOLUTION:
As mentioned above, values like minus sign '-' and the plus sign '+' by themselves will satisfy isnumeric() but cannot be used for the "is digit" query. For this, it is better to choose the wildcard approach
SELECT
FROM
(
select case
IsNull((select 1
where idnum not like '%[^0-9]%'),0)
when 1 then CAST(idnum as int)
end as digit_idnum
from my_table
) AS t
WHERE
digit_idnum >= 200 and
digit_idnum <= 700
If you find this solution useful, you are welcome to press one of the ads in this page.. Thanks!
No comments:
Post a Comment