Solved

SQL Syntax to check if value exists in any column

Posted on 2013-06-20
4
372 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
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

910 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

19 Experts available now in Live!

Get 1:1 Help Now