sql >> Base de Datos >  >> RDS >> Sqlserver

¿Cómo escribir una consulta SQL para este resultado?

declare @t table(Id int,seq varchar(100)) 
insert into @t (Id,seq) values (1,'1839073,'),(2,'1839073,1850098,'),(3,'1850099,1850100,1850110 ')



;With Cte as (
SELECT A.Id,  
     Split.a.value('.', 'VARCHAR(100)') AS Seq  
 FROM  
 (
     SELECT Id,  
         CAST ('<M>' + REPLACE(seq, ',', '</M><M>') + '</M>' AS XML) AS Data  
     FROM  @t
 ) AS A CROSS APPLY Data.nodes ('/M') AS Split(a) )

 Select ID,Seq from Cte Where Seq > ''