Solved

SQL Syntax to check if value exists in any column

Posted on 2013-06-20
4
369 Views
Last Modified: 2013-06-20
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
Comment
Question by:grantballantyne
  • 2
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 total points
Comment Utility
SELECT *
FROM MyTable
WHERE pAddress2 LIKE '%TD1%' OR pTown LIKE '%TD1%' OR pPostcode LIKE '%TD1%'

If you want the exaxt value, remove the %
0
 
LVL 11

Expert Comment

by:Simone B
Comment Utility
Try this:

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

Open in new window

0
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
That would only work for exact values, though.
0
 
LVL 11

Expert Comment

by:Simone B
Comment Utility
Yes, that's true. But when that's what you're looking for, it's very handy.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now