将一个长逗号字符串拆分为多列新表

时间:2013-06-11 09:35:19

标签: sql sql-server string sql-server-2008

我不喜欢SQL,请原谅任何无知。我有一个名为'temp'的表,其中包含一个带有一个长逗号分隔字符串的字段,因此:

Field1
Apples,oranges, pears, berries, melons

我想将上面的内容插入到现有的表'详细'中,该表有预定义的列,所以它看起来像这样:

Field1   Field2   Field3  Field4   Field5
Apples   Oranges  Pears   Berries  Melons 

我一直在阅读论坛,我认为我需要使用一个功能 - 但是我如何才能这样做呢?哪个功能最好?有人说XML功能最好吗?

此致

迈克尔

3 个答案:

答案 0 :(得分:1)

试试这个

;WITH Split_Fields (Field, xmlfields)
AS
(
    SELECT Field1 AS Field,
    CONVERT(XML,'<Fields><field>'  
    + REPLACE(Field1,',', '</field><field>') + '</field></Fields>') AS xmlfields
      FROM Table1
)

 SELECT Field,      
 xmlfields.value('/Fields[1]/field[1]','varchar(100)') AS Field1,    
 xmlfields.value('/Fields[1]/field[2]','varchar(100)') AS Field2,
 xmlfields.value('/Fields[1]/field[3]','varchar(100)') AS Field3,    
 xmlfields.value('/Fields[1]/field[4]','varchar(100)') AS Field4,
 xmlfields.value('/Fields[1]/field[5]','varchar(100)') AS Field5
 FROM Split_Fields

<强> SQLFiddle Demo

OR

SELECT Field,
xmlfields.value('/Fields[1]/field[1]','varchar(100)') AS Field1, 
xmlfields.value('/Fields[1]/field[2]','varchar(100)') AS Field2, 
xmlfields.value('/Fields[1]/field[3]','varchar(100)') AS Field3, 
xmlfields.value('/Fields[1]/field[4]','varchar(100)') AS Field4, 
xmlfields.value('/Fields[1]/field[5]','varchar(100)') AS Field5 
FROM 
(
SELECT 
Field1 AS Field
,CONVERT(XML,'<Fields><field>' + REPLACE(Field1,',', '</field><field>') + '</field></Fields>') AS xmlfields 
FROM Table1
) Split_Fields

插入另一个表(例如:详细信息),请尝试以下

;WITH Split_Fields (Field, xmlfields)
AS
( 
SELECT 
Field1 AS Field
,CONVERT(XML,'<Fields><field>' + REPLACE(Field1,',', '</field><field>') + '</field></Fields>') AS xmlfields 
FROM Table1
) 

INSERT INTO Detail
SELECT 
xmlfields.value('/Fields[1]/field[1]','varchar(100)') AS Field1, 
xmlfields.value('/Fields[1]/field[2]','varchar(100)') AS Field2, 
xmlfields.value('/Fields[1]/field[3]','varchar(100)') AS Field3, 
xmlfields.value('/Fields[1]/field[4]','varchar(100)') AS Field4, 
xmlfields.value('/Fields[1]/field[5]','varchar(100)') AS Field5 
FROM Split_Fields

答案 1 :(得分:0)

Oracle SQL:

SELECT  REPLACE(SUBSTR(r_str, 1, INSTR(r_str, ',', 1, 1) - 1), ' ') AS r_substr_1
,       REPLACE(SUBSTR(r_str, INSTR(r_str, ',', 1, 1) + 1, INSTR(r_str, ',', 1, 2) - INSTR(r_str, ',', 1, 1) - 1), ' ') AS r_substr_2
,       REPLACE(SUBSTR(r_str, INSTR(r_str, ',', 1, 2) + 1, INSTR(r_str, ',', 1, 3) - INSTR(r_str, ',', 1, 2) - 1), ' ') AS r_substr_3
,       REPLACE(SUBSTR(r_str, INSTR(r_str, ',', 1, 3) + 1, INSTR(r_str, ',', 1, 4) - INSTR(r_str, ',', 1, 3) - 1), ' ') AS r_substr_4
,       REPLACE(SUBSTR(r_str, INSTR(r_str, ',', 1, 4) + 1), ' ')    AS r_substr_5
FROM
(
        SELECT  'Apples, oranges, pears,berries, melons' r_str FROM DUAL
);

Result: Apples  oranges pears   berries melons

答案 2 :(得分:0)

通常sql用于结构化数据,逗号分隔的字符串将来自外部数据文件。如果您有可用的外部数据文件,请加载这样的数据。或者,如果必须,可以考虑将原始临时表导出到数据文件。

bulk insert tbl_fruits from file.csv with fieldterminator=','