Hi @Maxence C ,
1.Please import the flat file into the SQL table;
a. Do not tick "Column names in the first data row".
b. Set the column name as Category(Your desired column name).
c. Preview data.
d. Create new table in OLEDB Destination:
CREATE TABLE [Categories] (
Id int Identity(1,1),
Category varchar(50)
)
e. Mapping in Des and ignore Id.
2.Please use T-SQL to get the expected result in SSMS.(It is very convenient to use T-SQL to process data.)
a.
select * from Categories;
b.
;with cte as (
select Id,case when LEN(Category)-LEN(ltrim(Category))=0 then Category end [Level 3]
,case when LEN(Category)-LEN(ltrim(Category))=4 then ltrim(Category) else NULL end [Level 2]
,case when LEN(Category)-LEN(ltrim(Category))=8 then ltrim(Category) else NULL end [Level 1]
,case when LEN(Category)-LEN(ltrim(Category))=12 then ltrim(Category) else NULL end [Label]
from Categories)
,cte1 as (
select *,
max(case when [Level 3] is not null then Id end) over (order by Id) as maxid,
max(case when [Level 2] is not null then Id end) over (order by Id) as maxid1,
max(case when [Level 1] is not null then Id end) over (order by Id) as maxid2,
max(case when [Label] is not null then Id end) over (order by Id) as maxid3
from cte)
,cte2 as (
select
coalesce([Label], max([Label]) over (partition by maxid,maxid1,maxid2,maxid3)) as [Label],
coalesce([Level 1], max([Level 1]) over (partition by maxid,maxid1,maxid2)) as [Level 1],
coalesce([Level 2], max([Level 2]) over (partition by maxid,maxid1)) as [Level 2],
coalesce([Level 3], max([Level 3]) over (partition by maxid)) as [Level 3]
from cte1)
select * from cte2 where [Label] is not null
go
Best Regards,
Mona
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.