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 6814673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:42:59+00:00 2026-05-26T20:42:59+00:00

I have a query that is somewhat equal to the following: SELECT @address =

  • 0

I have a query that is somewhat equal to the following:

SELECT @address = a.Id FROM dbo.[Address] AS a, dbo.Item as u
    WHERE a.Name = u.Name 
            AND ISNULL(a.Number, '') = ISNULL(u.Number, '') 
        AND ISNULL(a.Floor, '') = ISNULL(u.Floor, '')
        AND ISNULL(a.Door, '') = ISNULL(u.Door, '')
        AND a.Zip = u.Zip
        AND u.ItemId = @id;

The whole idea is to find the addresses from dbo.Address that correspond to the dbo.Item table.

Now, the problem is that from this query I need to have the non-matching ones and determine the reason why they didn’t match example: we cannot find the Name, or the Number, or the Floor etc. I have been trying to accomplish this by using consecutive SELECTs:

SELECT @addressId = a.Id FROM dbo.[Address] AS a, dbo.Item as u
        WHERE a.Name = u.Name 
            AND u.ItemId = @id;
        -- Name not found           
        IF @addressId IS NULL
        BEGIN
            SET @retval = 4
            RETURN @retval;
        END
        ELSE
        BEGIN
            SELECT @addressId = a.Id FROM dbo.[Address] AS a, dbo.Item as u
            WHERE a.Name = u.Name 
                AND a.Number = ISNULL(u.HusNr, '')
                AND u.ItemId = @id;
            -- Number not found
            IF @addressId IS NULL
            BEGIN
                SET @retval = 5
                RETURN @retval;
            END
... and so on

but this is slow and I come to think that there’s got to be a more intelligent way to accomplish this.

NOTE(on Icarus’s answer):

The problem with this approach is that it tries to join several fields at once. Sql tries to join all the fields and thus will return NULL for the whole row, and not for the specific columns. If we want the specific columns we would have to do something like:

SELECT a.Id , 
       case when a.Name is null then 1
       when a.Number is null then 2
       when a.Floor is null and a.Door is null and a.Zip is null then 3
       when a.Number is not null and a. Floor is not null and a.Door is not null and a.Zip is not null then 0 end as Reason
FROM dbo.Item u1 left join dbo.[Address] a 
       ON a.Name = u1.Name left join dbo.Item u2
       ON a.Number = u2.Number left join dbo.Item u3
       ON a.Floor = u3.Floor left join dbo.Item u4
       ON a.Door = u4.Door left join dbo.Item u5
       ON a.Zip = u5.Zip
where  u.ItemId = @id;

…but then we get a bunch of results that we don’t need i.e. all the possible combinations, which again…we don’t need. and if we use the above CASE on that the first one is always true because the first result is all NULLs

  • 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-26T20:43:00+00:00Added an answer on May 26, 2026 at 8:43 pm

    You could return the reason code in a separate column as so:

    SELECT a.Id , 
           case when a.Name is null then 1
           when a.Number is null then 2
           when a.Floor is null and a.Door is null and a.Zip is null then 3
           when a.Number is not null and a. Floor is not null and a.Door is not null and a.Zip is not null then 0 end as Reason
    FROM dbo.[Address] a left join dbo.Item u
           on a.Name = u.Name 
           or a.Number = u.Number
           or a.Door = u.Door 
    where  u.ItemId = @id;
    

    Where Reason 0 above is a perfect match. You can define your reasons as you wish, my intention was to give you an idea on how to do this using a case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have a somewhat complex SQL query that is pulling from a table of
I have inherited a somewhat messy query that I am working on refactoring to
I have a query that I'm executing from a .NET application to a SQL
I have a PHP web app that generates a somewhat complex report from our
Ok SQL and Oracle gurus I have a somewhat complicated query that I'm trying
I have a query that UNION 's two somewhat similar datasets, but they both
I have a query like this: SELECT t1.id, (SELECT COUNT(t2.id) FROM t2 WHERE t2.id
I have a somewhat simple query in SQL that I would like to convert
I have a somewhat complex query that I want to use as a source

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.