Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Crea una riga separata se una colonna contiene un valore separato da virgole

Puoi scrivere una query come:

DECLARE @employee TABLE (FirstName  VARCHAR(100),
                         LastName   VARCHAR(100),
                         PayScale INT)

INSERT INTO @employee VALUES 
('Craig','L',150000) , 
('Alice,Lisa','simons',100000)



SELECT Split.a.value('.', 'VARCHAR(100)') AS FirstName  , LastName  ,PayScale
       FROM  (SELECT  CAST ('<M>' + REPLACE(FirstName, ',', '</M><M>') + '</M>' 
                             AS XML) AS FirstName ,  LastName    ,PayScale
               FROM  @employee) 
       AS A 
CROSS APPLY FirstName.nodes ('/M') AS Split(a)