Link to home
Create AccountLog in
Microsoft SQL Server

Microsoft SQL Server

--

Questions

--

Followers

Top Experts

Avatar of Dorababu M
Dorababu M🇮🇳

STRING_AGG alternative for SQL 2013

I have a query which is working with STRING_AGG but the deployment server has 2013 version so I would like to convert the piece of code to support that version how can I do that


DECLARE @StageOfPassage TABLE
    (
        OperationOfVessel NVARCHAR(MAX),
        ReportableEventId INT
    )


    INSERT INTO @StageOfPassage
    SELECT STRING_AGG(c.LookupDataName, ','),
           s.ReportableEventId
    FROM dbo.VesselDetail AS s
        CROSS APPLY STRING_SPLIT(s.OperationofVesselId, ',') AS f
        INNER JOIN dbo.LookupData AS c
            ON f.value = c.LookupDataId
    GROUP BY s.ReportableEventId;


   SELECT * FROM @StageOfPassage

Open in new window


Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of slightwv (䄆 Netminder)slightwv (䄆 Netminder)

Try the old STUFF and XML PATH trick.

There are MANY examples of this out there.

For example:
https://www.mssqltips.com/sqlservertip/2914/rolling-up-multiple-rows-into-a-single-row-and-column-for-sql-server-data/

Avatar of Dorababu MDorababu M🇮🇳

ASKER

Hi slightwv (䄆 Netminder) the problem I had is in one table the Id's are stored as comma separated values so I am a bit confused do apply those


Avatar of Dorababu MDorababu M🇮🇳

ASKER

Here is what I will have a master table and in the child table it stores the comma separated value as follows


Table1


Vid RId CommaId

  1     1       1,2

  2     2       5,7


Table2

ColId   ColName

   1          Test

   2          Test1

   3           Test2

   4           Test3

   5           Test4

   6           Test5

   7           Test6


I need to join these two so that the output will be like 


ColName          RId

  Test,Test1        1

  Test4,Test6        2


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Based on your last couple questions, I STRONGLY encourage you to not store data as a CSV in a column.

It only causes problems like in your last questions and doesn't follow the rules of normalization.

It also does things like prevent proper indexing and such.

It is just a bad design.

That said, try this:
select
	rid,
	stuff(csv,1,1,'')
from table1 t1
	cross apply (
		select ',' + colname
		from table2 t2
		where charindex(',' + cast(t2.colid as varchar(10)) + ',', ',' + t1.commaid + ',') > 0
		order by colid
		for xml path('')
	) x(csv)
group by rid, csv
;

Open in new window


Complete working fiddle:
https://dbfiddle.uk/6iP2Qcuu

Avatar of Dorababu MDorababu M🇮🇳

ASKER

Thank you is it possible to exclude NULL values?


Almost anything is possible.

If you cannot figure it out, can you update your test data and expected results to show where nulls are involved?

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Dorababu MDorababu M🇮🇳

ASKER

It will be the same like this


Vid RId CommaId

  1     1       1,2

  2     2       5,7

  3     3       NULL

  4     4       NULL  

  5     5       2,5 



It should result only 1, 2 and 5


ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)slightwv (䄆 Netminder)

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account
Microsoft SQL Server

Microsoft SQL Server

--

Questions

--

Followers

Top Experts

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.