SQL Syntax to check if value exists in any column

Dear Experts

I wondered if someone could advise me on the correct TSQL select syntax to check if a value exists in any one of three columns?

For example:  I have 3 columns 'pAddress2', 'pTown', 'pPostcode'

How could I check if the value 'TD1' exists in any of these columns and return the data.

Many thanks for any help
grantballantyneAsked:
Who is Participating?
 
CluskittConnect With a Mentor Commented:
SELECT *
FROM MyTable
WHERE pAddress2 LIKE '%TD1%' OR pTown LIKE '%TD1%' OR pPostcode LIKE '%TD1%'

If you want the exaxt value, remove the %
0
 
Simone BSenior E-Commerce AnalystCommented:
Try this:

SELECT COL1, COL2
FROM TABLENAME
WHERE 'TD1' IN (pAddress2, pTown, pPostcode)

Open in new window

0
 
CluskittCommented:
That would only work for exact values, though.
0
 
Simone BSenior E-Commerce AnalystCommented:
Yes, that's true. But when that's what you're looking for, it's very handy.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.