user1840255
user1840255

Reputation: 287

SQL Query Help - LIKE Statement

SELECT  Name
FROM    Names n
WHERE   EXISTS (SELECT NameIDs FROM NameList nl WHERE n.nameID LIKE '%'+nl.nameIDs);

I'm trying to query this table and it only seems to return the first values. Name list is a comma seperated value cell and there can be multiple occurences of it.

I was wondering if anyone could help.

Upvotes: 0

Views: 118

Answers (2)

CathalMF
CathalMF

Reputation: 10055

If its comma separated then you need to have the wildcard % after the nl.nameIDs too as the value can occurr anywhere in the middle of the comma separated string.

SELECT  Name
FROM    Names n
WHERE   EXISTS (SELECT NameIDs FROM NameList nl WHERE n.nameID LIKE '%'+nl.nameIDs+'%');

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1269773

I think you have the comparison backwards. Try this:

SELECT  Name
FROM    Names n
WHERE   EXISTS (SELECT NameIDs FROM NameList nl WHERE ','+nl.nameIDS+',' like '%,'+n.nameID+',%' 

Upvotes: 0

Related Questions