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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:40:35+00:00 2026-05-29T09:40:35+00:00

I have the need to have a inner join based on the value of

  • 0

I have the need to have a inner join based on the value of a parameter I have in a stored procedure. I’m also using a function to split values out of a string of comma separated values. My code is as follows

Select *
from view_Project as vp
join inline_split_me(@earmark) as e on (vp.EarmarkId LIKE e.Value and @earmark IS NOT NULL)

If @earmark is NULL then I don’t want this join to happen at all, otherwise if I have a string of ‘%’ or ‘119’ or ‘119,120,121’ this join should happen and does yield the proper results. I would just like to have it not happen at all if @earmark is null, I thought that I could just use the and @earmark is not null to delineate that however it is not returning the proper results, which is discovered by commenting the join line out and running the same sproc with null as the @earmark param, which gives me all rows as a result. When I keep this join and pass null I get no rows, I’ve been fiddling with this for some time, any help would be appreciated.

Here is the FUNCTION:

[inline_split_me](@param nvarchar(MAX))
RETURNS TABLE AS
RETURN(SELECT ltrim(rtrim(convert(nvarchar(4000),
    substring(@param, Number,
        charindex(N',' COLLATE SQL_Latin1_General_CP1_CI_AS,
        @param + convert(nvarchar(MAX), N','),
    Number) -
Number)
))) AS Value
   FROM   APM_Numbers
   WHERE  Number <= convert(int, len(@param))
     AND  substring(convert(nvarchar(MAX), N',') + @param, Number, 1) =
                    N',' COLLATE SQL_Latin1_General_CP1_CI_AS)

Got it, thanks Cade Roux and others

if (@earmark = '%')
select *
    from view_Project as vp
    where vp.EarmarkId like @earmark
else
select *
    from view_Project as vp
    where @earmark is null or vp.EarmarkId in (select Value from inline_split_me(@earmark))
  • 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-29T09:40:36+00:00Added an answer on May 29, 2026 at 9:40 am

    INNER JOIN is your problem. A LEFT JOIN will always return the rows on the LEFT, even though when @earmark is NULL, the join condition can never be true.

    Select *
    from view_Project as vp
    LEFT join inline_split_me(@earmark) as e on (vp.EarmarkId LIKE e.Value and @earmark IS NOT NULL)
    

    You could fool around with a UNION to manufacture rows to join when @earmark is NULL

    Select *
    from view_Project as vp
    INNER join (
        SELECT Value, -- columns here ...
        FROM inline_split_me(@earmark) as e
        UNION ALL
        SELECT DISTINCT vp.EarmarkId AS Value, -- NULL, NULL, etc.
        FROM view_Project
        WHERE @earmark IS NULL
    ) AS e
        ON vp.EarmarkId LIKE e.Value
    

    But frankly, I would just do a conditional logic:

    IF @earmark IS NULL
        Select *
        from view_Project as vp
    ELSE
        Select *
        from view_Project as vp
        INNER join inline_split_me(@earmark) as e on (vp.EarmarkId LIKE e.Value and @earmark IS NOT NULL)
    

    If you can get away from LIKE:

    Select *
    from view_Project as vp
    WHERE @earmark IS NULL OR vp.EarmarkId IN (
        SELECT Value FROM inline_split_me(@earmark)
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have outer div and inner div. I need to place inner div at
I have need to select a number of 'master' rows from a table, also
I have need for a function pointer that takes two arguments and returns a
I have a need to create a transactional process using an external API that
I have a stored procedure that pulls data for a report. I'm having a
I am using EF 4.0 and I need to implement query with one inner
I have a table in SQL Server of strings that i need to inner
I need help with a raw SQL query which gets a value based on
I have two tables that need to be joined based on matching xml. The
We have need for a rating system in a project we are working on,

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.