想要将逗号分隔值从1列放到另一列或同一列中

时间:2014-10-09 13:53:21

标签: sql sql-server arrays database function

此处,所有数据都是来自api的数据。
考虑到,有一个任务可以分配给一个或多个用户 - 左下图给出了TASK_TABLE:
TASK_ID,TASK_NAME,ASSIGNED_TO,CREATED_DATE,...等等。
现在ASSIGNED_TO列有数据(在下面提到)。
从这一栏我只想要逗号分隔的用户名,如 - 蝙​​蝠侠,超人,蜘蛛侠。 列 - >
ASSIGNED_TO:

 [  
      { 
        "id":"bf2b9698-3557-422c-8116-27f57e763e50",
        "profileUrl":"/products/people/profile.aspx?user=bat.man",
        "displayName":"bat man"
      },  
      {
        "id":"1f0acef8-5193-4daf-b42c-72c9cec8c284",
        "profileUrl":"/products/people/profile.aspx?user=super.man",
        "displayName":"super man"
      },  
      {
        "id":"217eacdd-ff80-4c2c-8e72-755a3e5bba44",
        "profileUrl":"/products/people/profile.aspx?user=spider.man",
        "displayName":"spider man"
      }
    ]

Actual_Table

id   name        assigned_to
1   Task1       (refer the above data)

表I我想要

id   name       assigned_to 
1   Task1       Bat Man,Super Man ,Spider Man

CREATE TABLE [dbo].[Test](
    [id] [int] NULL,
    [name] [nchar](10) NULL,
    [assigned_to] [varchar](max) NULL
)

INSERT INTO dbo.[Test] (
    [id]
    ,[name]
    ,[assigned_to]
    )
VALUES (
    1
    ,'task1'
    ,'[{"id":"bf2b9698-3557-422c-8116-27f57e763e50","profileUrl":"/products/people/profile.aspx?user=bat.man","displayName":"bat man"}
       ,{"id":"1f0acef8-5193-4daf-b42c-72c9cec8c284","profileUrl":"/products/people/profile.aspx?user=super.man","displayName":"super man"}
       ,{"id":"217eacdd-ff80-4c2c-8e72-755a3e5bba44","profileUrl":"/products/people/profile.aspx?user=spider.man","displayName":"spider man"}]'
    )
GO

0 个答案:

没有答案