Make a separate row if a column contains comma-separated value

后端 未结 1 911
青春惊慌失措
青春惊慌失措 2021-01-24 06:11

Let\'s suppose we have a simple select query which return result like below

   FirstName          LastName                     PayScale
   -----------        --         


        
1条回答
  •  囚心锁ツ
    2021-01-24 06:51

    You can write a query as:

    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 ('' + REPLACE(FirstName, ',', '') + '' 
                                 AS XML) AS FirstName ,  LastName    ,PayScale
                   FROM  @employee) 
           AS A 
    CROSS APPLY FirstName.nodes ('/M') AS Split(a)
    

    0 讨论(0)
提交回复
热议问题