Share via

How to write subquery in case when clause?

Y.N 80 Reputation points
2024-01-23T03:10:35.9033333+00:00

It seems impossible to add a subquery in a CASE statement.

SELECT CASE WHEN Column1 IN (SELECT Column2 FROM Table2) 
            THEN 'Y'ELSE 'N' END
FROM Table1

Any suggestions?

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

LiHongMSFT-4306 31,621 Reputation points
2024-01-23T03:18:36.6466667+00:00

Hi @Y.N Try this:

SELECT CASE WHEN EXISTS (SELECT 1 FROM Table2 B WHERE B.Column2 = A.Column1) 
            THEN 'Y' ELSE 'N' END
FROM Table1 A

Best regards,

Cosmog Hong


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.