使用MS SQL中的分隔PIPE转换为列

时间:2016-10-10 17:35:46

标签: sql-server-2005 pipe

现值

全部(一栏)

--

43 | 00043 | 22/09/2016 | Ooredoo - AuU TX | Raj singh 

我希望它应该在单独的列中,如下所示

ID  Key   Date       Company                   Owner 

43  00043 22/09/2016 Ooredoo - AuU TX         Raj singh

1 个答案:

答案 0 :(得分:0)

您可以使用以下方法执行此操作并在开头声明分隔符。这是一个样本:

DECLARE @Delimiter VARCHAR(40)
SET @Delimiter = '|'
;WITH CTE AS
(
    SELECT
        CAST('<M>' + REPLACE([ColName], @Delimiter , '</M><M>') + '</M>' AS XML)
        AS [ColName XML]
    FROM [Table]
)
SELECT
     [ColName XML].value('/M[1]', 'VARCHAR(40)') As [ID],
     [ColName XML].value('/M[2]', 'VARCHAR(40)') As [Key],
     [ColName XML].value('/M[3]', 'VARCHAR(40)') As [Date],
     [ColName XML].value('/M[4]', 'VARCHAR(40)') As [Company],
     [ColName XML].value('/M[5]', 'VARCHAR(40)') As [Owner]
FROM CTE