Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use like condition with multiple values in sql server 2005?

I need to filter out records based on some text matching in nvarchar(1000) column. Table has more than 400 thousands records and growing. For now, I am using Like condition:-

SELECT 
    *
FROM
    table_01
WHERE
    Text like '%A1%'
    OR Text like '%B1%'
    OR Text like '%C1%'
    OR Text like '%D1%'

Is there any preferred work around?

like image 419
User13839404 Avatar asked Jan 31 '11 20:01

User13839404


People also ask

How use LIKE operator in SQL for multiple values?

The SQL LIKE clause is used to compare a value to similar values using wildcard operators. There are two wildcards used in conjunction with the LIKE operator. The percent sign represents zero, one or multiple characters. The underscore represents a single number or character.

Can we use like for multiple values in SQL?

“The SQL LIKE operator allows performing logical evaluation for any matching records. Using the LIKE operator, you can specify single or multiple conditions. This allows you to perform an action such as select, delete, and updating any columns or records that match the specified conditions.

How do you do multiple conditions in like?

select * from employee where name like '%aa%' or name like '%bb% or name like '%cc%'..... Next row NAME Column Contains aa bb cc dd..... each value in the NAME column and put in the multiple like operator using OR condition.

How use NOT LIKE operator in SQL for multiple values?

So, here is the easiest solution. select * from table1 where column1 not like '%value1%' and column1 not like '%value2%' and column1 not like'%value3%'; If you want to play around with the Boolean logic, you rearrange the query like this.


2 Answers

SELECT 
    *
FROM
    table_01
WHERE
    Text like '%[A-Z]1%'

This will check if the texts contains A1, B1, C1, D1, ...

Reference to using the Like Condition in SQL Server

like image 195
John Hartsock Avatar answered Oct 03 '22 17:10

John Hartsock


You can try the following if you know the exact position of your sub string:

SELECT 
    *
FROM
    table_01
WHERE
    SUBSTRING(Text,1,2) in ('B1','C1','D1')
like image 40
Nusret Zaman Avatar answered Oct 03 '22 19:10

Nusret Zaman