LIKE and IN command in the same SQL command

0

Is it possible to use like and in in the same SQL command? For example, I need to search the stock table for the Serial Number that can be 'SCAB171293E29','SCAB171293E4E' but it might be that they have something else in the end so I thought of something like this:

S elect numeroserie from public.estoque where numeroserie like in ('SCAB171293E29%','SCAB171293E4E%')

But obviously it made a mistake. Are there any ways to accomplish this select ?

    
asked by anonymous 09.11.2017 / 18:30

3 answers

5

You can enter an array containing the beginning of the serial number you want to search as below:

select *
from estoque
where numero_serie like any(array['SCAB171293E29%','SCAB171293E4E%']);
    
09.11.2017 / 19:34
2

You need to separate in 3 conditions: numeroserie equal to 'SCAB171293E29' or 'SCAB171293E4E', or% with% similar to 'SCAB171293E29%', or serial number similar to 'SCAB171293E4E%'. The result would be:

select numeroserie from public.estoque where numeroserie in ('SCAB171293E29','SCAB171293E4E') or numeroserie like 'SCAB171293E29%' or numeroserie like 'SCAB171293E4E%'.

With more robust checks it is always interesting to think atomically to find the final solution. So you think of each scanning criterion separately and then it's easier to put them together for the final solution.

    
09.11.2017 / 18:35
0

Well if the serial number size is bigger, it does not make any difference since you are using the% (after all it works just for this)
Maybe you are expressing badly, I believe your doubt is, in some cases your in will have 3 serial numbers and in others will have 15, for example. You can do this:

SELECT * FROM TABELA WHERE numeroserie IN (SELECT NUMEROSERIE FROM OUTRA_TABELA)

If it is not already this, you must then separate it into several clauses. For example, when the size of the number is 5 you do with the IN, when it is 6, you do with the%. Here is an example:

SELECT * FROM TABELA WHERE numeroserie IN ('numeroserie','numeroserie2') OR numeroserie like '%numeros%' OR numeroserie like '%serie'
    
09.11.2017 / 19:22