如何将逗号分隔值拆分为行

时间:2014-01-22 00:11:22

标签: sql-server ssis

.csv文件有两列

 Main Category               Sub Category
--------------------------------------------------------                    
Technology/Internet    Computers/Internet,Technology/Internet,Software Downloads, Social media

我想将这两列列为

 Main Category               Sub Category
--------------------------------------------------------                    
Technology/Internet    Computers/Internet
Technology/Internet    Technology/Internet                  
Technology/Internet    Software Downloads
Technology/Internet    Social media

我尝试使用这个表达式: SUBSTRING(Sub Category,1,FINDSTRING(Sub Category,"-",1) + 1,LEN(Sub Category))  在SSIS的derived column中 但它不起作用 并且,我可以在SSIS

中放置while loop
Declare 
@start int,
@end int,
@category_group Varchar(250),
@sub_category varchar(250)

set @start = 1
set @end = 99
set @category_group =(select distinct([Sub_Category]) from [Site_Categories])
set @sub_category = 
(select replace(substring([Sub_Category],1,charindex(N',',[Sub_Category],1)),',','') from  [Site_Categories])

while 
@start <= @end
begin
insert into [dbo].[Category_group]
([category_group],[sub_category])
select 
@category_group,
@sub_category

set @start= @start+1
end

请在SSIS中解决此案例应该做些什么

3 个答案:

答案 0 :(得分:1)

您似乎有两个问题:1)拆分列子类别和2)将1行转换为2行。

问题1可以通过这种方式解决:

字符串的左侧部分:

SUBSTRING(Sub Category,1,FINDSTRING(Sub Category,",",1) - 1)

字符串的右侧部分:

SUBSTRING(Sub Category,FINDSTRING(Sub Category,",",1) + 1,LEN(Sub Category))

问题2可以用这个数据流来解决:

enter image description here

答案 1 :(得分:0)

答案 2 :(得分:0)

您的数据

DECLARE @TABLE TABLE ( MainCategory  NVARCHAR(100),SubCategory NVARCHAR(100))
INSERT INTO @TABLE VALUES
('Technology/Internet' ,   'Computers/Internet, Technology/Internet'),
('Technology/Intranet' ,   'Laptops/IPads, Laptops/Tablets')

<强>查询

SELECT T.MainCategory, Item AS SubCategory
FROM @TABLE t 
     CROSS APPLY 
       (SELECT * FROM dbo.Split(SubCategory, ','))S

结果集

╔═════════════════════╦══════════════════════╗
║    MainCategory     ║     SubCategory      ║
╠═════════════════════╬══════════════════════╣
║ Technology/Internet ║ Computers/Internet   ║
║ Technology/Internet ║  Technology/Internet ║
║ Technology/Intranet ║ Laptops/IPads        ║
║ Technology/Intranet ║  Laptops/Tablets     ║
╚═════════════════════╩══════════════════════╝

拆分功能

CREATE FUNCTION Split (  
      @InputString                  VARCHAR(8000),  
      @Delimiter                    VARCHAR(50)  
)  

RETURNS @Items TABLE (  
      Item                          VARCHAR(8000)  
)  

AS  
BEGIN  
      IF @Delimiter = ' '  
      BEGIN  
            SET @Delimiter = ','  
            SET @InputString = REPLACE(@InputString, ' ', @Delimiter)  
      END  

      IF (@Delimiter IS NULL OR @Delimiter = '')  
            SET @Delimiter = ','  

--INSERT INTO @Items VALUES (@Delimiter) -- Diagnostic  
--INSERT INTO @Items VALUES (@InputString) -- Diagnostic  

      DECLARE @Item                 VARCHAR(8000)  
      DECLARE @ItemList       VARCHAR(8000)  
      DECLARE @DelimIndex     INT  

      SET @ItemList = @InputString  
      SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)  
      WHILE (@DelimIndex != 0)  
      BEGIN  
            SET @Item = SUBSTRING(@ItemList, 0, @DelimIndex)  
            INSERT INTO @Items VALUES (@Item)  

            -- Set @ItemList = @ItemList minus one less item  
            SET @ItemList = SUBSTRING(@ItemList, @DelimIndex+1, LEN(@ItemList)-@DelimIndex)  
            SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)  
      END -- End WHILE  

      IF @Item IS NOT NULL -- At least one delimiter was encountered in @InputString  
      BEGIN  
            SET @Item = @ItemList  
            INSERT INTO @Items VALUES (@Item)  
      END  

      -- No delimiters were encountered in @InputString, so just return @InputString  
      ELSE INSERT INTO @Items VALUES (@InputString)  

      RETURN  

END -- End Function