The solution I am proposing requires each line to have a unique identification. Therefore, for the purpose of demonstrating the solution, it will be considered that such unique identification exists.
-- código #1
CREATE TABLE tabela (ID_sequencia tinyint identity, Sequencia varchar(100));
INSERT into tabela values ('0,10;2,2;5,13;8,4');
go
To separate the elements will be used the user function Delimitedsplit8k, authored by Jeff Moden. Information about this function is available in the article An Improved SQL 8K "CSV Splitter" Function, the source code of which may also be obtained in Split a list of values.
The first step is to separate each pair:
-- código #2
SELECT ID_sequencia, T.sequencia, S1.ItemNumber as seqPar, S1.Item as Par
from tabela as T
cross apply dbo.DelimitedSplit8K (T.sequencia, ';') as S1;
After separating each pair, each element of each pair should be obtained:
-- código #3
with
gPar as (
SELECT ID_sequencia, T.sequencia, S1.ItemNumber as seqPar, S1.Item as Par
from tabela as T
cross apply dbo.DelimitedSplit8K (T.sequencia, ';') as S1
),
gElemento as (
SELECT P.ID_Sequencia, P.sequencia, P.seqPar,
S2.ItemNumber as seqElemento, S2.Item as Elemento
from gPar as P
cross apply dbo.DelimitedSplit8K (P.Par, ',') as S2
)
SELECT ID_Sequencia, sequencia, seqPar, seqElemento, Elemento
from gElemento
order by ID_Sequencia, seqPar;
Having each separate element, the final step is to group the final elements of each pair in the order of each pair. Here is the complete SQL code, using the function STRING_AGG to group the elements:
-- código #4
-- Autor: José Diz
with
gPar as (
SELECT ID_sequencia, T.sequencia, S1.ItemNumber as seqPar, S1.Item as Par
from tabela as T
cross apply dbo.DelimitedSplit8K (T.sequencia, ';') as S1
),
gElemento as (
SELECT P.ID_Sequencia, P.sequencia, P.seqPar,
S2.ItemNumber as seqElemento, S2.Item as Elemento
from gPar as P
cross apply dbo.DelimitedSplit8K (P.Par, ',') as S2
)
SELECT ID_Sequencia,
string_agg (Elemento, ',') within group (order by seqPar)
from gElemento
where seqElemento = 2
group by ID_Sequencia;
Since the function STRING_AGG only works from the 2017 version of SQL Server, it can be replaced by the technique blackbox xml in previous versions of SQL Server. Details in article Concatenation of several lines in the same column.
-- código #5 v2
-- Autor: José Diz
with
gPar as (
SELECT ID_sequencia, T.sequencia, S1.ItemNumber as seqPar, S1.Item as Par
from tabela as T
cross apply dbo.DelimitedSplit8K (T.sequencia, ';') as S1
),
gElemento as (
SELECT P.ID_Sequencia, P.sequencia, P.seqPar,
S2.ItemNumber as seqElemento, S2.Item as Elemento
from gPar as P
cross apply dbo.DelimitedSplit8K (P.Par, ',') as S2
)
SELECT E.ID_Sequencia,
--string_agg (Elemento, ',') within group (order by seqPar)
stuff ((SELECT ',' + T.Elemento
from gElemento as T
where T.ID_Sequencia = E.ID_Sequencia
and T.seqElemento = 2
order by T.seqPar
for xml path(''), TYPE).value('.', 'varchar(max)'),
1, 1, '') as novaSequencia
from gElemento as E
where E.seqElemento = 2
group by E.ID_Sequencia;
Maybe this link can help - https://renenyffenegger.ch/notes/development/databases/SQL-Server/T-SQLfunct/regexp/index#:~:text=Apparently%2C%20SQL%20Server%20does%20not,create%20a%20regexp_replace()%20function.
– Rodrigo Zem
You can use "string split" type functions to initially separate the pairs by the delimiter ";" and then, in each pair, get the second element using the separator ",". See article Split a list of values -> https://portosql.wordpress.com/2019/01/27/separar-conteudo-de-texto-multivalorado_string-split/
– José Diz