Looking for an SQL table that already contains wildcards?

I have a table containing patches for phone numbers, where x can match any digit.

+ ---- + -------------- + ---------------------- +
| ID | phone_number | phone_number_type_id |
+ ---- + -------------- + ---------------------- +
| 1 | 1234x000x | 1 |
| 2 | 87654311100x | 4 |
| 3 | x111x222x | 6 |
+ ---- + -------------- + ---------------------- +

Now I can have 511132228, which will correspond to line 3, and it should return its type. So this is similar to SQL wilcards, but vice versa, and I'm confused about how to achieve this.

+4
source share
3 answers

Give this move:

select * from my_table
where '511132228' like replace(phone_number, 'x', '_')
+3
select * 
from yourtable
where '511132228' like (replace(phone_number, 'x','_'))
+3

:

SELECT ID,phone_number,phone_number_type_id
FROM TableName
WHERE '511132228' LIKE REPLACE(phone_number,'x','_');

:

With TableName as
(

SELECT 3 ID, 'x111x222x' phone_number, 6 phone_number_type_id from dual
)

SELECT 'true' value_available
FROM TableName
WHERE '511132228' LIKE REPLACE(phone_number,'x','_');

The above query returns data if a pattern match is available, and will not return a string if a match is not available.

0
source

Source: https://habr.com/ru/post/1544440/


All Articles