Results 1 to 5 of 5
  1. #1
    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.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    SELECT ...
      FROM ...
     WHERE 'r937' IN ( id_number, secondary_id_number )
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    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])

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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])
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Oct 2011
    Location
    Corpus Christi, TX
    Posts
    25
    Thanks r937!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •