SQL Server多次拆分单个列

时间:2011-06-22 13:08:57

标签: sql sql-server split

我有一个数据库表,其中包含一个堆叠数据的列,其中包含两个级别,其中包含一个我要拆分的列。这是数据的例子(改变数据以保护无辜者:):

ID = varchar(100)
CarData = varchar(1000)

ID       CarData
1        Nissan:blue:20000,Ford:green:10000
2        Nissan:steel:20001,Ford:blue:10001,Chevy:blue:10000,Ford:olive:10000
** Note that cardata can is not fixed, and can have many cars in it

输出所需:

ID   Manufacture    Color     Cost
1    Nissan         Blue      20000
1    Ford           green     10000
2    Nissan         steel      20001
... and on

所以说明白我需要打破第一个堆叠字段,这是一个逗号并为其创建一行,然后将第二个堆叠字段分解为列。

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:6)

-- Sample data
declare @T table(ID int, CarData varchar(100))
insert into @T values 
(1,        'Nissan:blue:20000,Ford:green:10000'),
(2,        'Nissan:steel:20001,Ford:blue:10001,Chevy:blue:10000,Ford:olive:10000')

-- Recursice CTE to get one row for each car
;with cte(ID, Car, CarData) as
(
  select ID,
         cast(substring(CarData+',', 1, charindex(',', CarData+',')-1) as varchar(100)),
         stuff(CarData, 1, charindex(',', CarData), '')+','
  from @T
  where len(CarData) > 0
  union all
  select ID,
         cast(substring(CarData, 1, charindex(',', CarData)-1) as varchar(100)),
         stuff(CarData, 1, charindex(',', CarData), '')
  from cte
  where len(CarData) > 0
)
-- Use parsename to split the car data
select ID,
       parsename(replace(Car, ':', '.'), 3) as Manufacture,
       parsename(replace(Car, ':', '.'), 2) as Color,
       parsename(replace(Car, ':', '.'), 1) as Cost
from cte
order by ID

结果:

ID  Manufacture  Color   Cost
--  -----------  ------   -----
1   Nissan       blue    20000
1   Ford         green   10000
2   Nissan       steel   20001
2   Ford         blue    10001
2   Chevy        blue    10000
2   Ford         olive   10000

修改1

如果颜色,费用或制造商名称包含parsename,您将无法使用.。如果是这种情况,你应该尝试这样做。

-- Sample data
declare @T table(ID int, CarData varchar(100))
insert into @T values 
(1,        'Nissan:blue:20000,Ford:green:10000'),
(2,        'Nissan:steel:20001,Ford:blue:10001,Chevy:blue:10000,Ford:olive:10000')

-- Recursice CTE to get one row for each car
;with cte(ID, Car, CarData) as
(
  select ID,
         cast(substring(CarData+',', 1, charindex(',', CarData+',')-1) as varchar(100)),
         stuff(CarData, 1, charindex(',', CarData), '')+','
  from @T
  where len(CarData) > 0
  union all
  select ID,
         cast(substring(CarData, 1, charindex(',', CarData)-1) as varchar(100)),
         stuff(CarData, 1, charindex(',', CarData), '')
  from cte
  where len(CarData) > 0
)
-- Split the car data with substring
select ID,
       substring(Car, 1, P1.Pos-1) as Manufacture,
       substring(Car, P1.Pos+1, P2.Pos-P1.Pos-1) as Color,
       substring(Car, P2.Pos+1, len(Car)-P2.Pos) as Cost
from cte
  cross apply (select charindex(':', Car)) as P1(Pos)
  cross apply (select charindex(':', Car, P1.Pos+1)) as P2(Pos)
order by ID

答案 1 :(得分:1)

使用this string splitting function生成结果表。

我首先使用dbo.split()作为分隔符调用,。然后你会有一个项目列表,如:

Nissan:blue:20000
Ford:green:10000
Nissan:steel:20001
Ford:blue:10001
Chevy:blue:10000
Ford:olive:10000

您可以使用dbo.split()作为分隔符再次致电:。每次调用都会产生三条记录(假设您的设计至少是“正常”)。

正如@JNK在评论中提到的那样,希望这不是你想要定期运行的东西。

修改

一些示例代码可帮助您入门:

SELECT *
INTO #YuckyCar
FROM (
  SELECT 1 ID, 'Nissan:blue:20000,Ford:green:10000' CarData
  UNION
  SELECT 2, 'Nissan:steel:20001,Ford:blue:10001,Chevy:blue:10000,Ford:olive:10000'
) T;

-- Shows logical step #1
SELECT ID, X.items MoreCarData
FROM #YuckyCar CROSS APPLY dbo.Split(CarData, ',') X;

-- Shows logical step #2
SELECT Q.ID, Y.items
FROM (
  SELECT ID, X.items MoreCarData
  FROM #YuckyCar CROSS APPLY dbo.Split(CarData, ',') X) Q CROSS APPLY dbo.Split(Q.MoreCarData, ':') Y

DROP TABLE #YuckyCar;

最后一部分的问题是你不能保证第1行=制造商,第2行=颜色,第3行=成本。

答案 2 :(得分:1)

这可以解决您的问题:

[编辑] 您的ID是varchar(100)并且您没有指定它是否是主键,所以我做了一些更改... ID不一定是主键在这种情况下。

declare @T table(ID varchar(100), CarData varchar(1000))
declare @OUT table(pk INT IDENTITY(1,1), ID varchar(100), Manufacture varchar(100), Color VARCHAR(100), Cost INT)
insert into @T (ID, CarData) values 
('1', 'Nissan:blue:20000,Ford:green:10000'),
('2', 'Nissan:steel:20001,Ford:blue:10001,Chevy:blue:10000,Ford:olive:10000')

DECLARE @x XML, @i INT, @ID VARCHAR(100), @maxi INT; 
;WITH list AS (SELECT pk = ROW_NUMBER() OVER(ORDER BY ID), * FROM @T)
  SELECT @i=1, @maxi=MAX(pk) FROM list;
WHILE @i <= @maxi
BEGIN
  ;WITH list AS (SELECT pk = ROW_NUMBER() OVER(ORDER BY ID), * FROM @T)
    SELECT
       @x = CAST( '<root><car><prop>' + 
                  REPLACE(
                    REPLACE(
                       CarData
                      ,':'
                      ,'</prop><prop>'
                    )
                    ,','
                    ,'</prop></car><car><prop>'
                  ) + 
                  '</prop></car></root>'
             AS XML)
     , @ID = ID 
    FROM list 
    WHERE pk = @i

  INSERT INTO @OUT 
    SELECT 
       ID = @ID
      ,Manufacture = x.value('./prop[1]','VARCHAR(100)')
      ,Color   = x.value('./prop[2]','VARCHAR(100)')
      ,Cost    = x.value('./prop[3]','INT')
    FROM @x.nodes('/root/car') AS T(x)

  SET @i = @i + 1;
END

SELECT * FROM @OUT

/* -- OUTPUT
ID  Manufacture   Color   Cost
--------------------------------
1   Nissan        blue    20000
1   Ford          green   10000
2   Nissan        steel   20001
2   Ford          blue    10001
2   Chevy         blue    10000
2   Ford          olive   10000
*/