Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using 'LIKE' with an 'IN' clause full of strings

I want to do a soft string match in a table, like this:

SELECT * FROM emailaddresses where addr in ('[email protected]', '[email protected]')

But if there is an addr value in the table '[email protected]', I want that returned.

Sort of like this:

SELECT * FROM emailaddresses where addr LIKE in ('[email protected]', '[email protected]')

How do I do that?

like image 947
John Shedletsky Avatar asked Apr 05 '11 17:04

John Shedletsky


3 Answers

put the values into a table and use a join rather than an in clause:

SELECT * FROM emailaddresses as ea
INNER JOIN addresses as a
ON ea.address like '%' + a.address + '%'
like image 199
Abe Miessler Avatar answered Oct 14 '22 09:10

Abe Miessler


You can use the LOWER function

SELECT * FROM emailaddresses where LOWER(addr) in ('[email protected]', '[email protected]')

Which will convert all addr to lowercase, in which you can then compare the results to what you want.

like image 27
Mike Lewis Avatar answered Oct 14 '22 07:10

Mike Lewis


Note that LIKE will work either case-sensitively or case-insensitively depending upon which collation is in effect for the expression, but in your case, you have specified no wildcards so there is little point looking to use LIKE.

The default SQL Server installation is case-insensitive.

If you want a case-insensitive compare because you've got a case-sensitive database, you can cast. I believe this is the appropriate syntax (I've never used it for an IN list on one side of an expression, though).

SELECT *
FROM emailaddresses
WHERE addr COLLATE SQL_Latin1_General_CP1_CI_AS 
IN (
    '[email protected]' COLLATE SQL_Latin1_General_CP1_CI_AS
    ,'[email protected]' COLLATE SQL_Latin1_General_CP1_CI_AS
)

A real case for LIKE would be something for something like addr LIKE '%@google.com"

like image 36
Cade Roux Avatar answered Oct 14 '22 08:10

Cade Roux