Solved

SQL Syntax to check if value exists in any column

Posted on 2013-06-20
4
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 total points
ID: 39263309
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
ID: 39263317
Try this:

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

Open in new window

0
 
LVL 18

Expert Comment

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

Expert Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

730 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