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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:23:28+00:00 2026-05-30T08:23:28+00:00

Consider a situation we have two variables in SQL Server 2005’s SP as below,

  • 0

Consider a situation we have two variables in SQL Server 2005’s SP as below,

@string1 = 'a,b,c,d'
@string2 = 'c,d,e,f,g'

Is there a solution to get a new string out of that like (@string1 U @string2) without using any loops. i.e the final string should be like,

@string3 = 'a,b,c,d,e,f,g'
  • 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-30T08:23:29+00:00Added an answer on May 30, 2026 at 8:23 am

    In case you need to do this as a set and not one row at a time. Given the following split function:

    USE tempdb;
    GO
    CREATE FUNCTION dbo.SplitStrings(@List nvarchar(max))
    RETURNS TABLE
    AS
       RETURN ( SELECT Item FROM
           ( SELECT Item = x.i.value(N'./text()[1]', N'nvarchar(max)')
             FROM ( SELECT [XML] = CONVERT(xml, '<i>'
             + REPLACE(@List,',', '</i><i>') + '</i>').query('.')
               ) AS a CROSS APPLY [XML].nodes('i') AS x(i) ) AS y
           WHERE Item IS NOT NULL
       );
    GO
    

    Then with the following table and sample data, and string variable, you can get all of the results this way:

    DECLARE @foo TABLE(ID INT IDENTITY(1,1), col NVARCHAR(MAX));
    
    INSERT @foo(col) SELECT N'c,d,e,f,g';
    INSERT @foo(col) SELECT N'c,e,b';
    INSERT @foo(col) SELECT N'd,e,f,x,a,e';
    
    DECLARE @string NVARCHAR(MAX) = N'a,b,c,d';
    
    ;WITH x AS
    (
        SELECT f.ID, c.Item FROM @foo AS f
        CROSS APPLY dbo.SplitStrings(f.col) AS c
    ), y AS
    (
        SELECT ID, Item FROM x
        UNION
        SELECT x.ID, s.Item
            FROM dbo.SplitStrings(@string) AS s
            CROSS JOIN x
    )
    SELECT ID, Items = STUFF((SELECT ',' + Item 
        FROM y AS y2 WHERE y2.ID = y.ID 
        FOR XML PATH(''), TYPE).value(N'./text()[1]', N'nvarchar(max)'), 1, 1, N'')
    FROM y
    GROUP BY ID;
    

    Results:

    ID   Items
    --   ----------
     1   a,b,c,d,e,f,g
     2   a,b,c,d,e
     3   a,b,c,d,e,f,x
    

    On newer versions (SQL Server 2017+), the query is much simpler, and you don’t need to create your own custom string-splitting function:

    ;WITH x AS
    (
        SELECT f.ID, c.value FROM @foo AS f
        CROSS APPLY STRING_SPLIT
        (
          CONCAT(f.col, N',', @string), N','
        ) AS c GROUP BY f.ID, c.value
    )
    SELECT ID, STRING_AGG(value, N',')
    WITHIN GROUP (ORDER BY value)
    FROM x GROUP BY ID;
    
    • Example db<>fiddle

    Now that all said, what you really should do is follow the previous advice and store these things in a related table in the first place. You can use the same type of splitting methodology to store the strings separately whenever an insert or update happens, instead of just dumping the CSV into a single column, and your applications shouldn’t really have to change the way they’re passing data into your procedures. But it sure will be easier to get the data out!

    EDIT

    Adding a potential solution for SQL Server 2008 that is a bit more convoluted but gets things done with one less loop (using a massive table scan and replace instead). I don’t think this is any better than the solution above, and it is certainly less maintainable, but it is an option to test out should you find you are able to upgrade to 2008 or better (and also for any 2008+ users who come across this question).

    SET NOCOUNT ON;
    
    -- let's pretend this is our static table:
    
    CREATE TABLE #x
    (
        ID int IDENTITY(1,1),
        col nvarchar(max)
    );
    
    INSERT #x(col) VALUES(N'c,d,e,f,g'), (N'c,e,b'), (N'd,e,f,x,a,e');
    
    -- and here is our parameter:
    
    DECLARE @string nvarchar(max) = N'a,b,c,d';
    

    The code:

    DECLARE @sql nvarchar(max) = N'DECLARE @src TABLE(ID INT, col NVARCHAR(32));
        DECLARE @dest TABLE(ID int, col nvarchar(32));';
    
    SELECT @sql += '
        INSERT @src VALUES(' + RTRIM(ID) + ','''
        + REPLACE(col, ',', '''),(' + RTRIM(ID) + ',''') + ''');'
    FROM #x;
    
    SELECT @sql += '
        INSERT @dest VALUES(' + RTRIM(ID) + ','''
        + REPLACE(@string, ',', '''),(' + RTRIM(ID) + ',''') + ''');'
    FROM #x;
    
    SELECT @sql += '
        WITH x AS (SELECT ID, col FROM @src UNION SELECT ID, col FROM @dest)
        SELECT DISTINCT ID, Items = STUFF((SELECT '','' + col
         FROM x AS x2 WHERE x2.ID = x.ID FOR XML PATH('''')), 1, 1, N'''')
         FROM x;'
    
    EXEC sys.sp_executesql @sql;
    GO
    DROP TABLE #x;
    

    This is much trickier to do in 2005 (though not impossible) because you need to change the VALUES() clauses to UNION ALL…

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

Sidebar

Related Questions

Consider a situation. I have an in-proc COM server that contains two COM classes.
Consider the following situation: We have two Localizable.string files, one in en.lproj and one
This is a complex question, please consider carefully before answering. Consider this situation. Two
Consider a situation. We have some specific C++ compiler, a specific set of compiler
Consider the following situation. I have 3 tables in a resource planning project(created using
Consider a situation: I have a buffer of known length that presumably stores a
Consider the situation i have a table name test ------- content (varchar(30)) ------- 1
Consider this situation where we have one product Laptop adapter and its stock as
We have one perplexity about current situation with TFS. There are 3 branches: Develop,
Consider situation when we have a class with couple of static methods that use

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.