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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:49:04+00:00 2026-06-13T07:49:04+00:00

I need to do a JOIN with a ‘near match’. The best way to

  • 0

I need to do a JOIN with a ‘near match’. The best way to explain this is with an example:

CREATE TABLE Car
 (
 Vin int,
 Make nvarchar(50),
 ColorID int,
 )

CREATE TABLE Color
 (
 ColorID int,
 ColorCode nvarchar(10)
 )

CREATE TABLE ColorName
 (
 ColorID int,
 Languagecode varchar(12),
 ColorName nvarchar(50)
 )

INSERT INTO Color Values (1, 'RED CODE')
INSERT INTO Color Values (2, 'GREEN CODE')
INSERT INTO Color Values (3, 'BLUE CODE')

INSERT INTO ColorName Values (1, 'en', 'Red')
INSERT INTO ColorName Values (1, 'en-US', 'Red, my friend')
INSERT INTO ColorName Values (1, 'en-GB', 'Red, my dear')
INSERT INTO ColorName Values (1, 'en-AU', 'Red, mate')
INSERT INTO ColorName Values (1, 'fr', 'Rouge')
INSERT INTO ColorName Values (1, 'fr-BE', 'Rouge, mon ami')
INSERT INTO ColorName Values (1, 'fr-CA', 'Rouge, mon chum')

INSERT INTO Car Values (123, 'Honda', 1)

The SPROC would look like this:

DECLARE @LanguageCode varchar(12) = 'en-US'

SELECT * FROM Car A
JOIN Color B ON (A.ColorID = B.ColorID)
LEFT JOIN ColorName C ON (B.ColorID = C.ColorID AND C.LanguageCode = @LanguageCode)

See http://sqlfiddle.com/#!6/ac24d/24 (thanks to Jake!)

Here is the challenge:
When the SPROC parameter @LanguageCode is an exact match, all is well.

I would like for it to also work for partial matches; more specifically: say for example that @LanguageCode would be ‘en-NZ’ then I would like the SPROC to return the value for language code ‘en’ (since there is no value for ‘en-NZ’).

As an extra challenge: if there is no match at all I would like to return the ‘en’ value; for example if @LanguageCode would be ‘es’ then the SPROC would return the ‘en’ value (since there is no value for ‘es’).

  • 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-13T07:49:05+00:00Added an answer on June 13, 2026 at 7:49 am

    As @Roman Pekar has said in his comment, this can indeed be done, including your additional request about falling back to en, in one statement with the help of a ranking function. Here’s how you could go about it:

    WITH FilteredAndRanked AS (
      SELECT
        *,
        rnk = ROW_NUMBER() OVER (
          PARTITION BY ColorID
          ORDER BY CASE LanguageCode
            WHEN @LanguageCode          THEN 1
            WHEN LEFT(@LanguageCode, 2) THEN 2
            WHEN 'en'                   THEN 3
          END
        )
      FROM ColorName
      WHERE LanguageCode IN (
        @LanguageCode,
        LEFT(@LanguageCode, 2),
        'en'
      )
    )
    SELECT
      ...
    FROM       Car               A
    INNER JOIN Color             B ON (A.ColorID = B.ColorID)
    LEFT  JOIN FilteredAndRanked C ON (B.ColorID = C.ColorID AND C.rnk = 1)
    

    ;

    That is, the ColorName table is filtered and ranked before being used in the query, and then only the rows with the rankings of 1 are joined:

    1. The filter for ColorName includes only rows with LanguageCode values of @LanguageCode, LEFT(@LanguageCode, 2) and 'en'.

    2. The ranking values are assigned based on which language code each row contains: rows with LEFT(@LanguageCode, 2) are ranked after those with @LanguageCode but before the 'en' ones.

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

Sidebar

Related Questions

I need to join two tables called MSISDN and RANGES. Example: MSISDN table: MSISDN
I need to join this mySQL table: TABLE1 id pagetitle 1 remodeling 2 handywork
given the first table ,i need to join this table with the same table
I have 3 tables I need to join. The contracts table is the main
I have a table with more than 100 columns. I need to join it
I need to create dynamic INNER JOIN (I believe I need to anyway). I
I need to JOIN a table when certain conditions are met and LEFT JOIN
I'm trying to create a query and need to join against something that I
I need use join 2 table show all data. Exam Table Contact numCode |
I'm writing a report with Propel and need to join the same table multiple

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.