• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

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
0
grantballantyne
Asked:
grantballantyne
  • 2
  • 2
1 Solution
 
CluskittCommented:
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 BCommented:
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 BCommented:
Yes, that's true. But when that's what you're looking for, it's very handy.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now