【问题标题】:MS SQL split value (text qualifier + column determiter)MS SQL 拆分值(文本限定符 + 列限定符)
【发布时间】:2017-10-05 12:58:45
【问题描述】:

美好的一天

我有这样的表:

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,"\"","\"\"")

你知道如何进行真正的选择或更新吗?

谢谢。

【问题讨论】:

  • 说实话,我认为最好的选择是重新导入您的 CSV 并选择双引号作为文本限定符,这样它们就不会出现在您的列中,而不是运行查询来清理它们之后。
  • @Jacob H 我试过我有 XY CSVs..help.pragmaticworks.com/dtsxchange/scr/…

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


【解决方案1】:

您可以像这样使用查询:

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

【讨论】:

  • 递归查询“rec_cte”的“c”列中的锚点和递归部分的类型不匹配。消息 240,级别 16,状态 1,行 1 类型不匹配递归查询“rec_cte”的列“c1”中的锚点和递归部分。
  • 我更正了脚本。你能显示table1的定义吗?
【解决方案2】:

类似的路线:

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    相关资源
    最近更新 更多