4

I am writing a query in which I am trying to search a subquery/CTE for a wildcard substring, and nesting this logic in my CASE statement. For example:

SELECT
CASE 
WHEN '%' + text + '%' IN (SELECT Column1 FROM Table) THEN 'I am in Column1'
ELSE text END
FROM Table

Unfortunately, it looks like there is no possibly way to do this. Since I would need to use the LIKE operator and there is no way to use both LIKE and IN. I would have to write each LIKE statement separately, and that would be for 1000+ rows. Does anyone recommend a more immediate solution? Thanks kindly in advance!

-- Edit: Sorry, some clarifications per comments. A better example:

UserID     |  UserPeers   |  Gender
--------------------------------------------
Mike       |  Tom1, Bob1  |  M
John       |  Tom1, Greg1 |  M
Sally      |Mike1, John1  |  F
Sara       | Sally1, Bob1 |  F

In the above table, I need to search the substrings in UserPeers columns to see if they exist anywhere in the UserID column. The rows that would be successfully returned in this case would be the ones under Sally and Sara, since 'Mike' and 'Sally' exist under UserID.

SELECT *
FROM Users
WHERE '%' + UserPeers + '%' LIKE (SELECT UserID FROM Users)

The error returned here is: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

3
  • 1
    Please clarify with some example data. What about ANDing the conditions? Commented Mar 31, 2013 at 22:15
  • Not sure I follow. Do you just want to compare the text column with Column1 in the same row? Is text a variable or a column name? Commented Mar 31, 2013 at 22:15
  • Thank you all for the extensive responses, Aaron's initial answer did the trick. Got stuck on the LIKE. Thanks a bunch! Commented Mar 31, 2013 at 22:49

4 Answers 4

3
SELECT UserID, CASE WHEN EXISTS 
(
  SELECT 1 FROM dbo.Users WHERE UserPeers LIKE '%' + u.UserID + '%'
) THEN 'I am in Column1' ELSE UserID END
FROM dbo.Users AS u;
Sign up to request clarification or add additional context in comments.

Comments

0

Here is one approach:

SELECT (CASE WHEN exists (select 1 from table t2 where t2.column1 like '%' + t.text + '%')
             then 'I am in Column1'
             ELSE t.text
        END)
FROM Table t;

Your original query seemed to have the wildcards on the wrong side of the like.

Comments

0

You could try something like this:

select case when (select count(*) from table where column1 like ('%' + text + '%')) > 0 then 'I am in column1' else text end from table

1 Comment

Count can be quite expensive and wasteful when you don't care about the actual count. EXISTS is guaranteed to be no worse but can often be much better.
0

Probably you don't really need Like and IN


SELECT
CASE 
WHEN EXISTS (SELECT 1 FROM Table B WHERE B.COLUMN1 LIKE '%' + A.TEXT+ '%') THEN 'I am in Column1'
ELSE A.text END
FROM Table A 

3 Comments

Don't you mean to correlate the subquery?
Yes, I mean correlate the subquery, but I'm not sure @thoughtExperiment need a key=key.
Not sure either. If I were given these requirements I would have thrown them back; in this case all we can do is guess. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.