MS SQL拆分值(文本限定符+列限制器)

时间:2017-10-05 12:58:46

标签: sql sql-server database sql-server-2008

美好的一天

我有这样的表:

Table1

463,"Prawn,Ging","NONE","22","22","Africa,Japan,China","01/01/1999 - 10/04/2017","NONE.
462,"GOLD,Fish","NONE","22","22","China","01/01/1999 - 10/04/2017","NONE.
461,"Long Dog","NONE","22","22","USA,France,Italy,Canada","01/01/1999 - 10/04/2017","NONE.
460,"Cat","NONE","22","22",,,,"NONE.

我需要桌子或像这样选择

Table1
Column1     Column2            Column3  Column3 Column4      Column5                       Column6              Column7
463         Prawn,Ging         NONE       22     22      Africa,Japan,China       01/01/1999 - 10/04/2017        NONE.
462         GOLD,Fish          NONE       22     22      China                    01/01/1999 - 10/04/2017        NONE.
461         Long Dog           NONE       22     22      USA,France,Italy,Canada  01/01/1999 - 10/04/2017        NONE.
460         Cat                NONE       22     22                                                              NONE.

我看了How to fix the embedded text qualifier issue while exporting data to CSV flat file?

但我觉得解决这个问题的最佳选择是使用功能REPLACE(short_description," \""," \" \"&#34 ;)

您是否知道如何进行真正的选择或更新?

谢谢。

2 个答案:

答案 0 :(得分:0)

您可以使用以下查询:

if object_id('dbo.Table1') is not null
  drop table dbo.Table1
go
create table dbo.Table1(column1 nvarchar(max))

insert into Table1
select '463,"Prawn,Ging","NONE","22","22","Africa,Japan,China","01/01/1999 - 10/04/2017","NONE.'
union all select '462,"GOLD,Fish","NONE","22","22","China","01/01/1999 - 10/04/2017","NONE.'
union all select '461,"Long Dog","NONE","22","22","USA,France,Italy,Canada","01/01/1999 - 10/04/2017","NONE.'
union all select '460,"Cat","NONE","22","22",,,,"NONE.'

;with rec_cte as(
select substring(t1.column1, 1, t2.pos - 1) as id
     , substring(t1.column1, 1, t2.pos - 1) as c
     , replace(replace(substring(t1.column1, t2.pos + 2, 4000), ',,', ',"",'), ',,', ',"",') + '"' as c1
     , 1 as rn
  from Table1 t1
    cross apply (select charindex(',', t1.column1)) t2(pos)
union all
select t1.id
     , substring(t1.c1, 1, t2.pos - 1) as c
     , substring(t1.c1, t2.pos + 3, 4000) as c1
     , t1.rn + 1 as rn
  from rec_cte t1
    cross apply (select charindex('"', t1.c1)) t2(pos)
  where t2.pos > 0)

select [1] as colimn1
     , [2] as colimn2
     , [3] as colimn3
     , [4] as colimn4
     , [5] as colimn5
     , [6] as colimn6
     , [7] as colimn7
     , [8] as colimn8
from (
select id, c, rn
from rec_cte) src
pivot (max(c) for rn in ([1],[2],[3],[4],[5],[6],[7],[8])) as pvt

答案 1 :(得分:0)

沿着类似的路线:

declare @tmp table
(
bigtext varchar(400)
);
INSERT INTO @tmp
VALUES
('463,"Prawn,Ging","NONE","22","22","Africa,Japan,China","01/01/1999 - 10/04/2017","NONE.'),
('462,"GOLD,Fish","NONE","22","22","China","01/01/1999 - 10/04/2017","NONE.'),
('461,"Long Dog","NONE","22","22","USA,France,Italy,Canada","01/01/1999 - 10/04/2017","NONE.'),
('460,"Cat","NONE","22","22",,,,"NONE.');

update @tmp SET bigtext = REPLACE(bigtext,',,','##');
update @tmp SET bigtext = REPLACE(bigtext,',"','#');
update @tmp SET bigtext = REPLACE(bigtext,'"','');

with cte( bigtext, c, position, single ) as (
select bigtext
     , STUFF( bigtext, 1, CHARINDEX('#', bigtext + ' #'), '') AS c
     , 1 AS position
     , convert(nvarchar(max),left(bigtext, CHARINDEX('#', bigtext + ' #') -1)) AS single
  from @tmp
 union all
select bigtext
     , STUFF(c, 1, CHARINDEX('#', c + ' #'), '')
     , position + 1
     , convert(nvarchar(max),left(c, CHARINDEX('#', c + ' #') -1))
  from cte
 where c > ''
)

SELECT pvt.bigtext
     , [1]
     , [2]
     , [3]
     , [4]
     , [5]
     , [6]
     , [7]
     , [8]
 FROM 
( SELECT bigtext
       , single
       , position
    FROM cte ) AS src
PIVOT
(
    MAX( single )
    FOR position IN ( [1], [2], [3], [4], [5], [6], [7], [8] )

) AS pvt;