拆分逗号分隔的表列并将其转换为单个列

时间:2014-06-30 07:14:35

标签: sql sql-server

我有一个包含逗号分隔列的表。我想将该列转换为所有逗号分隔值的单个列。如何在单列中拆分每个groupid的Access列?

接下来是代码块:

    eg. 
`CREATE TABLE [dbo].[GROUP](
[GroupID] [int] NULL,
[MstID] [int] NULL,
[GroupName] [varchar](300) NULL,
[Access] [varchar](4000) NULL
  )

   INSERT INTO [dbo].[GROUP] values (1, 1,           'Administrator','Crr|IF|view,Crr|IF|Arc,Crr|IF|DL,Crr|IF|M2F,Crr|IF|Route,Crr|IF|print,Crr|IF|Add Crr,Crr|IF|Reply,Crr|ST|view,Crr|ST|Arc,Crr|ST|M2F,Crr|ST|prt,Crr|Arc|view,Crr|A rc|PArc,Crr|Arc|DL,Crr|Arc|M2F,Crr|Arc|prt,ADM|SET|view,ADM|SET|upd,ADM|FLD|view ,ADM|FLD|add,ADM|FLD|edit,ADM|FLD|del,ADM|Usr|view,ADM|Usr|add,ADM|Usr|edit,ADM| Usr|del,ADM|Usr|VA,ADM|UG|view,ADM|UG|add,ADM|UG|edit,ADM|UG|del,ADM|AT|view,ADM |AT|Arc,ADM|AT|E2E,ADM|AAT|view,ADM|AAT|PArc,ADM|AAT|RST,ADM|CP|view,ADM|CP|upda   te,Crr|DR|view,Crr|Srh|view')`

1 个答案:

答案 0 :(得分:0)

尝试使用 REPLACE 命令:

`INSERT INTO [dbo].[GROUP] values (1,   1,           'Administrator',REPLACE(REPLACE('Crr|IF|view,Crr|IF|Arc,Crr|IF|DL,Crr|IF|M2F,Crr|IF|Route,Crr|IF|print,Crr|IF|Add Crr,Crr|IF|Reply,Crr|ST|view,Crr|ST|Arc,Crr|ST|M2F,Crr|ST|prt,Crr|Arc|view,Crr|A rc|PArc,Crr|Arc|DL,Crr|Arc|M2F,Crr|Arc|prt,ADM|SET|view,ADM|SET|upd,ADM|FLD|view ,ADM|FLD|add,ADM|FLD|edit,ADM|FLD|del,ADM|Usr|view,ADM|Usr|add,ADM|Usr|edit,ADM| Usr|del,ADM|Usr|VA,ADM|UG|view,ADM|UG|add,ADM|UG|edit,ADM|UG|del,ADM|AT|view,ADM |AT|Arc,ADM|AT|E2E,ADM|AAT|view,ADM|AAT|PArc,ADM|AAT|RST,ADM|CP|view,ADM|CP|upda   te,Crr|DR|view,Crr|Srh|view', ',',''), '|', ''))`