create table Temp (ID int, Reference varchar(10))
insert into Temp select 1 ,'Ref1'
insert into Temp select 1 ,'Ref2'
insert into Temp select 1 ,'Ref3'
insert into Temp select 2 ,'RefA'
insert into Temp select 2 ,'RefB'
select * from Temp
select
b.id
, b.Reference
, a.ReferenceUnsplitted
from
( select ID, STRING_AGG(replace(reference,'Ref',''), '-') as ReferenceUnsplitted from temp group by ID ) a
join
Temp b on a.id = b.id
SELECT DISTINCT
T.ID
,T.Reference + X.[Value] AS Reference
,X.[Value] AS ExtraReference
FROM YourTable T
CROSS APPLY STRING_SPLIT(ExtraReference, ';') X;
If your really want to do an update the big problem is your table does not have a primary key.
(This is not allowed in relational theory.)
To get around this you are going to have to add a column to make a primary key and use a string split function that returns an index. As the enable_ordinal option for STRING_SPLIT is only available in Azure and SQL2022 I would use:
WITH TableWithPK
AS
(
-- ID, MakePK will be the primary key for the update
SELECT ID, Reference, ExtraReference
,ROW_NUMBER() OVER (PARTITION BY ID, Reference ORDER BY ID) AS MakePK
FROM YourTable
)
,ExtraSplits
AS
(
SELECT DISTINCT T.ID, T.Reference, X.ItemNumber, X.Item
FROM YourTable T
CROSS APPLY dbo.DelimitedSplit8K(ExtraReference, ';') X
)
UPDATE P
SET Reference = P.Reference + S.Item
,ExtraReference = S.Item
FROM TableWithPK P
JOIN ExtraSplits S
ON P.ID = S.ID
AND P.MakePK = S.ItemNumber
-- make idempotent
WHERE RIGHT(P.Reference, LEN(P.extraReference)) <> P.extraReference;
select * from YourTable;