Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7016189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:45:08+00:00 2026-05-27T22:45:08+00:00

Notice the 2 queries below. The 1st one does a union on 4 queries.

  • 0

Notice the 2 queries below. The 1st one does a union on 4 queries. I’m trying to write #2 for search conditions based on the 3 SQL variables prefixed with an “@”. So rather than doing a union, we have to take all 3 parameters into consideration for the search. And if any parameter/variable is ” (or NULL), just ignore that condition, but still perform the search. But all fields have to combine with each other for a single row/record in the search results.

How do I re-write QUERY #2 so that it pulls results based on the search conditions (@companyName, @primaryPhone and @postalCode)? I think each section in the where clause has to have some OR condition (so it doesn’t skip the row for a ”/NULL search condition), but I’m curious how this is typically done. The @primaryPhone part of the where clause is a little trickier because it looks at both phone and fax.

QUERY #1

SELECT tempTable.optionValue, tempTable.optionText FROM (   

    SELECT 
        address.addressid AS 'optionValue', 
        address.name AS 'optionText'
    FROM 
        dbo.address 
    WHERE 
        addressid=1

    UNION

    -- Company Name internal partial match
    SELECT 
        address.addressid AS 'optionValue', 
        ('[' + CAST(address.addressid AS NVARCHAR(10)) + '] ' + ISNULL(address.name,'') + ', ' + ISNULL(address1,'') + ', ' + ISNULL(city,'') + ' ' + ISNULL(statecode,'') + ', ' + ISNULL(countrycode,'') + ' ' + ISNULL(postalcode,'')) AS 'optionText'
    FROM 
        dbo.[address]
        LEFT OUTER JOIN dbo.contact_address ON dbo.address.addressid = dbo.contact_address.addressid
        LEFT OUTER JOIN dbo.clientcontact ON dbo.contact_address.contactid = dbo.clientcontact.contactid
        LEFT OUTER JOIN dbo.client ON dbo.clientcontact.clientid = dbo.client.clientid
        LEFT OUTER JOIN dbo.contact ON dbo.contact_address.contactid = dbo.contact.contactid
    WHERE 
        client.name IS NOT NULL 
        AND client.name != ''
        AND @companyName != ''
        AND @companyName IS NOT NULL
        AND client.name LIKE '%' + @companyName + '%' 
        AND clientcontact.contacttypeid = 3 --primary contacts only

    UNION

    -- Primary Phone/Fax internal partial match     
    SELECT 
        address.addressid AS 'optionValue', 
        ('[' + CAST(address.addressid AS NVARCHAR(10)) + '] ' + ISNULL(address.name,'') + ', ' + ISNULL(address1,'') + ', ' + ISNULL(city,'') + ' ' + ISNULL(statecode,'') + ', ' + ISNULL(countrycode,'') + ' ' + ISNULL(postalcode,'')) AS 'optionText'
    FROM 
        dbo.[contact]
        LEFT OUTER JOIN dbo.clientcontact ON dbo.contact.contactid = dbo.clientcontact.contactid
        LEFT OUTER JOIN dbo.contact_address ON dbo.contact.contactid = dbo.contact_address.contactid
        LEFT OUTER JOIN dbo.address ON dbo.contact_address.addressid = dbo.address.addressid
    WHERE 
        (
            contact.dayphone IS NOT NULL 
            AND contact.dayphone != ''
            AND @primaryPhone != ''
            AND @primaryPhone IS NOT NULL
            AND contact.dayphone LIKE '%' + @primaryPhone + '%'
        )
        OR 
        (
            contact.fax IS NOT NULL 
            AND contact.fax != ''
            AND @primaryPhone != ''
            AND @primaryPhone IS NOT NULL
            AND contact.fax LIKE '%' + @primaryPhone + '%' 
        )
        AND clientcontact.contacttypeid = 3 --primary contacts only

    UNION

    SELECT 
        address.addressid AS 'optionValue', 
        ('[' + CAST(address.addressid AS NVARCHAR(10)) + '] ' + ISNULL(address.name,'') + ', ' + ISNULL(address1,'') + ', ' + ISNULL(city,'') + ' ' + ISNULL(statecode,'') + ', ' + ISNULL(countrycode,'') + ' ' + ISNULL(postalcode,'')) AS 'optionText'
    FROM 
        dbo.[contact]
        LEFT OUTER JOIN dbo.clientcontact ON dbo.contact.contactid = dbo.clientcontact.contactid
        LEFT OUTER JOIN dbo.contact_address ON dbo.contact.contactid = dbo.contact_address.contactid
        LEFT OUTER JOIN dbo.address ON dbo.contact_address.addressid = dbo.address.addressid
    WHERE 
        @postalCode != ''
        AND @postalCode IS NOT NULL
        AND address.postalcode LIKE @postalCode + '%'
        AND clientcontact.contacttypeid = 3 --primary contacts only

) AS tempTable

QUERY #2 (SEARCH)

SELECT tempTable.optionValue, tempTable.optionText FROM (   

    SELECT 
        address.addressid AS 'optionValue', 
        address.name AS 'optionText'
    FROM 
        dbo.address 
    WHERE 
        addressid=1

    UNION

    SELECT 
        address.addressid AS 'optionValue', 
        ('[' + CAST(address.addressid AS NVARCHAR(10)) + '] ' + ISNULL(address.name,'') + ', ' + ISNULL(address1,'') + ', ' + ISNULL(city,'') + ' ' + ISNULL(statecode,'') + ', ' + ISNULL(countrycode,'') + ' ' + ISNULL(postalcode,'')) AS 'optionText'
    FROM 
        dbo.[address]
        LEFT OUTER JOIN dbo.contact_address ON dbo.address.addressid = dbo.contact_address.addressid
        LEFT OUTER JOIN dbo.clientcontact ON dbo.contact_address.contactid = dbo.clientcontact.contactid
        LEFT OUTER JOIN dbo.client ON dbo.clientcontact.clientid = dbo.client.clientid
        LEFT OUTER JOIN dbo.contact ON dbo.contact_address.contactid = dbo.contact.contactid
    WHERE 
        (
            client.name IS NOT NULL 
            AND client.name != ''
            AND @companyName != ''
            AND @companyName IS NOT NULL
            AND client.name LIKE '%' + @companyName + '%' 
        )
        AND 
        (
            (
                contact.dayphone IS NOT NULL 
                AND contact.dayphone != ''
                AND @primaryPhone != ''
                AND @primaryPhone IS NOT NULL
                AND contact.dayphone LIKE '%' + @primaryPhone + '%'
            )
            OR 
            (
                contact.fax IS NOT NULL 
                AND contact.fax != ''
                AND @primaryPhone != ''
                AND @primaryPhone IS NOT NULL
                AND contact.fax LIKE '%' + @primaryPhone + '%' 
            )
        )
        AND
        (
            @postalCode != ''
            AND @postalCode IS NOT NULL
            AND address.postalcode LIKE @postalCode + '%'
        )                       
        AND clientcontact.contacttypeid = 3 --primary contacts only

) AS tempTable
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:45:08+00:00Added an answer on May 27, 2026 at 10:45 pm

    EDIT New Rules

                         |  Field = '' or NULL  |  Field != '' or NULL
    ---------------------+----------------------+-----------------------
    Param  = '' or NULL  |    Include Record    |    Include Record
    ---------------------+----------------------+-----------------------
    Param != '' or NULL  |    Exclude Record    |   Include if Match
    

    WHERE 
        (
               @companyName = ''
            OR @companyName IS NULL
            OR client.name LIKE '%' + @companyName + '%' 
        )
        AND 
        (
            (
                   @primaryPhone = ''
                OR @primaryPhone IS NULL
                OR contact.dayphone LIKE '%' + @primaryPhone + '%'
            )
            OR 
            (
                   @primaryPhone = ''
                OR @primaryPhone IS NULL
                OR contact.fax LIKE '%' + @primaryPhone + '%' 
            )
        )
        AND
        (
               @postalCode = ''
            OR @postalCode IS NULL
            OR address.postalcode LIKE @postalCode + '%'
        )                       
        AND clientcontact.contacttypeid = 3 --primary contacts only
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my HomeController I have the following 3 queries, notice that the 3rd one
Notice how StackOverflow displays some nice Status Bar message based on your activity? (That
Notice: You have an error in your SQL syntax; check the manual that corresponds
I understand that mysqlnd allows non blocking queries. However, I'm trying: // goes without
I want to chain the contents of two queries. Please notice I DON'T want
I've notice most sources say to best practice to execute SQL statements in Python
I have been playing about with LINQ-SQL, trying to get re-usable chunks of expressions
I hand coded a simple SQL in SQL Server 2008 as below; SELECT *
I have a handful of raw SQL queries for SQL Server which use SCOPE_IDENTITY
I'm trying to do Xpath queries on DOMElements but it doesn't seem to work.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.