Is there always only one value that matches and needs deleted, or could there be multiple substrings that need deleted? That is, is something like this possible?:
select replace(string ,value+ ';','') as goat, *
from (
select *
from @temp
cross apply string_split(string, ';')
) a
cross apply DelimitedSplit8K(a.value,':') d
where d.Item = @stringid
If you're using SQL Server 2017 or above, this can be simplified a bit by changing the concatenation of the XML PATH stuff to STRING_AGG(). Are you using 2017 or above?
I don't assume a really recent version of SQL Server, since most people seem not to have one. Where I'm DBA, I only have one instance left at 2012 (SSRS), the rest are all 2016/2017, but that's not that common, probably mostly due to licensing costs.
Yep... I get that. Absolutely no slight intended in my question to the OP.
I'm kind of in the same boat as most people and don't have anything at work greater than 2017 and so I definitely get it. We'll have a need for folks to post the XML PATH method of concatenation for the next 4 to 8 years and totally appreciate why you posted that as a solution. I'd have done the same thing.
It's kind of like the days when 2005 and ROW_NUMBER() first came out. Shoot, I still post some 2005/2008 solutions because, similar to what you said, some people don't even have 2012 in place yet.
Bottom line is, I'm right there with you and, unless one has some prior knowledge of otherwise (and the OP on this thread provided none), it's almost always better to post something that will work in the most versions, like you did. You'll never need to justify that to me.
I was just asking the OP if they had a later version... just like I did back when 2005 first came out.
The previous repliers are all very knowledgeable (far more than I am) and their answers look good for what you specifically asked. However, I can't help noticing that the data in your strings looks like it should be a 4-column table, which might make the underlying problem much easier to solve, using standard SQL manipulations instead of string manipulations, which are much messier and harder to maintain. Regards.