将csv字段拆分为多个列

时间:2014-09-28 13:35:56

标签: sql-server csv

我有一个包含200,000行数据的表。现场参加者已填入csv格式;用逗号分隔符分隔。我想将这个字段分成多达7个不同的列,标记为Field1,Field2等。

示例数据:

pkEventBooking  Attendees
166935          p1193,c21867,c21827,c21963,c18069,c19222,
195867          p1193,c21827,c22572,c19222,c22573,c21963,c18069,

新格式

pkEventBooking   Field1   Field2   Field3    Field 4   Field5   Field6  Field7
166935           p1193    c21867   c21827    c21963    c18069   c19222
195867           p1193    c21827   c22572    c19222    c22573   c21963  c18069,

2 个答案:

答案 0 :(得分:0)

评论太长了。

我倾向于以逗号分隔的格式导出数据并重新导入。有了这么多数据,你可以做到以下几点:

  1. 运行select * from exampleselect pkEventBooking + ',' + Attendees
  2. 将数据复制到Excel
  3. 转到"数据"功能区并选择"文本到列"
  4. 保存为文件
  5. 使用制表符分隔符导入文件
  6. 顺便说一下,我真的建议您将数据结构化为两列:

    • pkEventBooks
    • 与会者

    然后有多列。

    您可以直接在数据库中执行此操作:

    insert into EventAttendees(EventBookId, Attendee)
        select t.EventBookId, ss.Attendee
        from table t cross apply
             dbo.SplitString(Attendees) as ss(Attendee);
    

    你可以google" SQL Server splitstring"得到这样一个函数的定义。

答案 1 :(得分:0)

测试数据

DECLARE @TABLE TABLE (pkEventBooking INT,  Attendees NVARCHAR(MAX))
INSERT INTO @TABLE VALUES 
(166935 , 'p1193,c21867,c21827,c21963,c18069,c19222'),
(195867 , 'p1193,c21827,c22572,c19222,c22573,c21963,c18069')

查询

;WITH Split_Names (pkEventBooking, Attendees)
AS
(
 SELECT pkEventBooking,
       CONVERT(XML,'<Attendees><Attendee>'  
   + REPLACE(Attendees,',', '</Attendee><Attendee>') + '</Attendee></Attendees>') AS Attendees
 FROM @Table
)

 SELECT pkEventBooking,      
 Attendees.value('/Attendees[1]/Attendee[1]','varchar(100)') AS Attendees1,    
  Attendees.value('/Attendees[1]/Attendee[2]','varchar(100)') AS Attendees2,
   Attendees.value('/Attendees[1]/Attendee[3]','varchar(100)') AS Attendees3,
    Attendees.value('/Attendees[1]/Attendee[4]','varchar(100)') AS Attendees4,
     Attendees.value('/Attendees[1]/Attendee[5]','varchar(100)') AS Attendees5,
      Attendees.value('/Attendees[1]/Attendee[6]','varchar(100)') AS Attendees6,
       Attendees.value('/Attendees[1]/Attendee[7]','varchar(100)') AS Attendees7
 FROM Split_Names

结果

╔════════╦════════════╦════════════╦════════════╦════════════╦════════════╦════════════╦════════════╗
║ Value  ║ Attendees1 ║ Attendees2 ║ Attendees3 ║ Attendees4 ║ Attendees5 ║ Attendees6 ║ Attendees7 ║
╠════════╬════════════╬════════════╬════════════╬════════════╬════════════╬════════════╬════════════╣
║ 166935 ║ p1193      ║ c21867     ║ c21827     ║ c21963     ║ c18069     ║ c19222     ║ NULL       ║
║ 195867 ║ p1193      ║ c21827     ║ c22572     ║ c19222     ║ c22573     ║ c21963     ║ c18069     ║
╚════════╩════════════╩════════════╩════════════╩════════════╩════════════╩════════════╩════════════╝