I want to write an SQL statement like below:
select * from tbl where col like ('ABC%','XYZ%','PQR%');
I know it can be done using OR
. But I want to know is there any better solution.
I want to write an SQL statement like below:
select * from tbl where col like ('ABC%','XYZ%','PQR%');
I know it can be done using OR
. But I want to know is there any better solution.
SELECT * From tbl WHERE col LIKE '[0-9,a-z]%';
simply use this condition of like in sql and you will get your desired answer
This might help:
I've used this in SQL Server 2005 and it worked.
I also had the same requirement where I didn't have choice to pass like operator multiple times by either doing an OR or writing union query.
Here is an alternative way:
Here is the test code to verify:
This is a good use of a temporary table.
In the example patterns, there's no way
col
could match more than one pattern, so you can be sure you'll see each row oftbl
at most once in the result. But if your patterns are such thatcol
could match more than one, you should use theDISTINCT
query modifier.If your parameter value is not fixed or your value can be null based on business you can try the following approach.
SplitString function