If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Query Question (searching two columns)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Oct 2011
Location: Corpus Christi, TX
Posts: 25
Query Question (searching two columns)

How can I run a query that searches for a reference number that may appear in two different columns? I want to pull all records for idenification numbers that can also show up in a secondary identification column.
Reply With Quote
  #2 (permalink)  
Old
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 20,000
Code:
SELECT ...
  FROM ...
 WHERE 'r937' IN ( id_number, secondary_id_number )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Oct 2011
Location: Corpus Christi, TX
Posts: 25
Thanks. Worked like a charm.

Can I pull this data from 6 tables at the same time?

SELECT *
FROM DBO.[NG 201107 - 201112]
WHERE '2760561238' IN ([Column 0],[Column 26])
Reply With Quote
  #4 (permalink)  
Old
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 20,000
sure, using UNIONs
Code:
SELECT *
  FROM DBO.[NG 201107]
 WHERE '2760561238' IN ([Column 0],[Column 26])
UNION ALL
SELECT *
  FROM DBO.[NG 201108]
 WHERE '2760561238' IN ([Column 0],[Column 26])
UNION ALL
...
SELECT *
  FROM DBO.[NG 201112]
 WHERE '2760561238' IN ([Column 0],[Column 26])
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old
Registered User
 
Join Date: Oct 2011
Location: Corpus Christi, TX
Posts: 25
Thanks r937!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On