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

  • Home
  • SEARCH
  • 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 8430021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:29:43+00:00 2026-06-10T05:29:43+00:00

The Problem I’m trying to write a stored procedure in SQL Server to find

  • 0

The Problem

I’m trying to write a stored procedure in SQL Server to find the best matching record. Given 5 input parameters @A, @B, @C, @D, and @E (all varchar50) that correspond to the 5 columns A, B, C, D, and E in my table, I’d like to find the record with the most matching columns. Each column that doesn’t match in the chosen record should contain a space ‘ ‘.

For example, if I have the input “Sony”, “PlayStation”, “Controller”, “Black”, “Damaged”, and my table contains the following columns:

"Sony"  "Playstation"  "Unit"   "Black"  "Damaged"
"Sony"  "Playstation"  " "      " "      " "

It should return the second row, because 2 parameters match and for the 3 that don’t, there are spaces. I don’t want to return the first row because even though 4 parameters match, the middle one does not match and it is not a space. If it had been a space, the first row would have been the winner.

My Approach

There are various specifics that I cannot reveal, but my basic approach (note that I’m an SQL novice) was to test every combination from MOST specific to LEAST specific. So my query would look something like this:

-- start with most specific
SELECT * FROM dbo.Items WHERE
   A = @A
   B = @B
   C = @C
   D = @D
   E = @E

-- if no matches, try next
IF @@ROWCOUNT = 0
SELECT * FROM dbo.Items WHERE
   A = @A
   B = @B
   C = SPACE(1)
   D = @D
   E = @E

... etc.

In my case, I only need to really test 16 configurations, because some of the permutations will never exist. Even so, this seems like a very inefficient way to achieve what I want. On top of that, it’s not even working. It seems like comparing against spaces is problematic because of some auto-trimming that’s going on. In any case, my current approach seems inefficient and it doesn’t work – so I turn to you for help.

  • 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-06-10T05:29:45+00:00Added an answer on June 10, 2026 at 5:29 am

    Something like this?

    -- Temp table to play with
    SELECT 1 AS Id, 'Sony' AS A, 'Playstation' AS B, 'Unit' AS C, 'Black' AS D, 
        'Damaged' AS E
    INTO #Items
    UNION SELECT 2, 'Sony', 'Playstation', ' ', ' ', ' '
    
    -- The query
    DECLARE @a nvarchar(50), @b nvarchar(50), @c nvarchar(50), @d nvarchar(50), @e 
    
    nvarchar(50)
    SET @a = 'Sony'
    SET @b = 'Playstation'
    SET @c = 'Controller'
    SET @d = 'Black'
    SET @e = 'Damaged'
    
    SELECT TOP 1 I.*
    FROM (
        SELECT ID,
            CASE WHEN @a = a THEN 1 WHEN a = ' ' THEN 0 ELSE NULL END AS AResult,
            CASE WHEN @b = b THEN 1 WHEN b = ' ' THEN 0 ELSE NULL END AS BResult,
            CASE WHEN @c = c THEN 1 WHEN c = ' ' THEN 0 ELSE NULL END AS CResult,
            CASE WHEN @d = d THEN 1 WHEN d = ' ' THEN 0 ELSE NULL END AS DResult,
            CASE WHEN @e = e THEN 1 WHEN e = ' ' THEN 0 ELSE NULL END AS EResult
        FROM #Items
    ) IW
    INNER JOIN #Items I ON I.ID = IW.ID
    WHERE AResult IS NOT NULL AND BResult IS NOT NULL AND CResult IS NOT NULL 
        AND DResult IS NOT NULL AND EResult IS NOT NULL
    ORDER BY AResult + BResult + CResult + DResult + EResult DESC
    

    That should return this value:

    "Sony"  "Playstation"  " "      " "      " "
    

    If you change the temp table I played with to this:

    SELECT 1 AS Id, 'Sony' AS A, 'Playstation' AS B, ' ' AS C, 'Black' AS D, 
        'Damaged' AS E
    INTO #Items
    UNION SELECT 2, 'Sony', 'Playstation', ' ', ' ', ' '
    

    Then you should get

    "Sony"  "Playstation"  " "   "Black"  "Damaged"
    

    And finally, if you have this for your temp table example:

    SELECT 1 AS Id, 'Sony' AS A, 'Playstation' AS B, 'Unit' AS C, 'Black' AS D, 
        'Damaged' AS E
    INTO #Items
    UNION SELECT 2, 'Sony', 'Playstation', 'Unit', ' ', ' '
    

    Nothing will get returned, since both have ‘Unit’ in the third column.

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

Sidebar

Related Questions

Problem: Find people whose birthdays are tomorrow (table a), who havent got a record
Problem: Given a list of strings, find the substring which, if subtracted from the
Problem Given a boolean expression consisting of the symbols 0, 1, &, |, ^
Problem: I am trying to build a recursive tree using a function and data
Problem! I Have the following input (rules) from a flat file (talking about numeric
Problem is that when I exec sql insert query (I'm using Qt Creator and
Problem Statement : Execute Various Command randomly by matching its percentage. like execute CommandA
Problem, simple and annoying. Im just trying to print a list of names, collected
Problem: Trying to create a Mix that is applied to the AVPlayerItem, but it
Problem: I need to retrieve the language of a given cell from the cube.

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.