Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm Concatenate string values using Stuff function. But this takes more time to execute.When I google, I came to know two ways 1.Indexing and 2. Cross Apply both Method I implemented in my query. But still it takes more time. How can i optimize the stuff function? Is there any other solution Available?

What I have tried:

SQL
SELECT p1.Data,
		STUFF((SELECT '-'+ Res 
               FROM View1 p2
               WHERE p2.Id = p1.Id 
			   Order by __seq__ID
               FOR XML PATH(''))
            ,1,1,'') AS 'Newcolumn'
		
FROM View1 p1
Posted
Updated 4-Feb-18 6:21am
v3
Comments
David_Wimbley 29-Jan-18 1:04am    
Not sure if applicable given i dont know what kind of data you are attempting to inject using STUFF nor do I know anything about your schema but have you tried using SUBSTRING and REPLACE functions instead of STUFF?
Developer29 29-Jan-18 1:09am    
can you give some example
David_Wimbley 29-Jan-18 1:38am    
Given I don't know anything about your schema/data nor do i have access to it you are better off google SUBSTRING and REPLACE functions to see if they meet your needs.
ZurdoDev 29-Jan-18 10:08am    
You can try COALESCE.

DECLARE @string NVARCHAR (MAX);
SELECT @string = COALESCE(@string+ '-', '') + Res
FROM wherever
Richard Deeming 30-Jan-18 12:33pm    
Concatenating Row Values in Transact-SQL[^]

Various alternative methods in that article. But we don't have access to your database, so we can't tell you which one will be fastest, or how to make them run faster.

If the name View1 means you're selecting from a view, then you probably need to optimise the view as well as the function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900