2

I have .csv file have two columns

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

I'd like to make these two columns like that

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

I tried to use this expression: SUBSTRING(Sub Category,1,FINDSTRING(Sub Category,"-",1) + 1,LEN(Sub Category)) in derived column in SSIS but it dose not work and, can I put while loop like that in SSIS

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

please what I should do to fix this Case in SSIS

user3143565
  • 63
  • 1
  • 7

3 Answers3

1

It seems that you have two problems: 1) Split the column subcategory and 2) Convert 1 row into 2 rows.

problem 1 can be solved this way:

left part of the string:

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

right part of the string:

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

problem 2 can be solved with this data flow:

enter image description here

Jayvee
  • 10,670
  • 3
  • 29
  • 40
  • SUBSTRING(Sub Category,1,FINDSTRING(Sub Category,",",1) - 1) – user3143565 Jan 22 '14 at 09:48
  • THIS give me error but if I have more than 5 sub categories in the same column – user3143565 Jan 22 '14 at 09:49
  • If you have a variable number of sub categories then the best option is a script component. You will need to make it asynchronous as well (i.e. from one row in the input pipeline you'll output many downstream) – Jayvee Jan 22 '14 at 10:09
  • SUBSTRING([Sub Category],1,FINDSTRING([Sub Category],",",1)-1) Give me error I don't know why – user3143565 Jan 22 '14 at 10:11
  • that's probably because there is only one sub category and therefore no comma, or it starts with a comma. It can be tested with this:FINDSTRING([Sub Category],",",1) > 1 ? SUBSTRING([Sub Category],1,FINDSTRING([Sub Category],",",1) - 1) : [Sub Category]. You will still have a problem with more than 2 sub categories though. – Jayvee Jan 22 '14 at 10:33
  • HI, Can we make it in ssis by foreach loop container to cut more than 5 sub categories @Jayvee – user3143565 Jan 23 '14 at 03:27
  • for each loop is a control flow task, is not available for data flows. Script is the best option. Otherwise you can preprocess the input file making it comma-separated with fixed number of columns and then use Unpivot to convert columns into rows. – Jayvee Jan 23 '14 at 07:56
0

Your Data

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')

Query

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

Result Set

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

Split Function

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 
M.Ali
  • 67,945
  • 13
  • 101
  • 127