我有一个包含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,
答案 0 :(得分:0)
评论太长了。
我倾向于以逗号分隔的格式导出数据并重新导入。有了这么多数据,你可以做到以下几点:
select * from example
或select pkEventBooking + ',' + Attendees
顺便说一下,我真的建议您将数据结构化为两列:
然后有多列。
您可以直接在数据库中执行此操作:
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 ║
╚════════╩════════════╩════════════╩════════════╩════════════╩════════════╩════════════╩════════════╝