I have an SQL Table with a column that stores keywords like this
keyword|44500903-8f09-40d8-a908-5fb3e03d145e;keyword2|441fb756-ff0a-473f-ad70-2f78d679e7d9
I need remove all characters between Pipe (including this) and semicolon, including this. The result must be:
Keyword; keyword2
Any ideas? I try to use, substring, trim etc... but didn't work.
2条答案
按热度按时间2uluyalo1#
Assuming you are on SQL Server 2022, then you can use
STRING_SPLIT
to split the values into rows on the semicolon (;
) with an ordinal position, then useLEFT
andCHARINDEX
to get the value up to the pipe (|
), and finally reaggregate withSTRING_AGG
:If you aren't on SQL Server 2022 you could use a different solution to split your values, such as a JSON splitter (2016+) or
delimitedsplit8k_LEAD
(2012+). If you aren't on SQL Server 2017+ you'll also need to switch outSTRING_AGG
for the "old"FOR XML PATH
(andSTUFF
) solution.dfddblmv2#
Here is a method that will work starting from SQL Server 2012 onwards.
We are tokenizing input string of tokens as XML. An intermediate result in t1(c) contains XML as follows:
We just need to retrieve XML elements values in odd positions by using XPath predicate
[position() mod 2 = 1]
.SQL
Output