You can convert all the numbers to a canonical format using translate()
and replace()
. From the canonical format, you can define the upper and lower bound on the range, depending on whether the phone has 10 or 14 characters:
select t.*
from t cross apply
(values (case when phone like '(+61)%'
then stuff(replace(translate(t.phone, '()+-', ' '), ' ', ''), 1, 2, '')
else replace(translate(t.phone, '()+-', ' '), ' ', '')
end)
) v(canonical)
(values (left(canonical, 10),
(case when len(canonical) = 10 then canonical
else left(canonical, 6) + right(canonical, 4)
end)
)
) v2(phone_upper, phone_lower)
Then your conditions are:
where @phone between v2.phone_lower and v2.phone_upper
I would advise you to figure out how to fix the data model. This is a really, really, really bad way to store phone numbers and phone number ranges.