0

I really would appreciate someone's help I`m a newbie in all of this SQL-SSIS stuff I have the following table in SQL:

enter image description here

And I need to create a table with columns that contain the AreaPath column and parsed by the delimiter \ Meaning my final goal is this:

enter image description here

Couple of important things:

  1. The original table is constantly changing (every 10 minutes)
  2. I have SQL 2014
  3. I can use SSIS to do so (I prefer to, than SQL)

Any help would be appreciated Thanks!

Hadi
  • 36,233
  • 13
  • 65
  • 124
Lena Meer
  • 79
  • 9
  • You'll get quicker help if you replace the images with consumable data like so, https://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ – Mazhar Jan 11 '18 at 09:47
  • Possible duplicate of [T-SQL split string based on delimiter](https://stackoverflow.com/questions/21768321/t-sql-split-string-based-on-delimiter) – Valerica Jan 11 '18 at 09:53

1 Answers1

0

If you use the string delimit function from this link and use PIVOT

Then the following works

DECLARE @Delim NVARCHAR(32) = '\';

DECLARE @t TABLE ( AreaSK INT, AreaName NVARCHAR(100), AreaPath NVARCHAR(100))
INSERT INTO @t
(AreaSK, AreaName, AreaPath)
VALUES
(12188, 'Kryptonite', '\Kryptonite'),
(12191, 'SL_A', '\Kryptonite\SL_A'),
(12192, 'P_Bender', '\Kryptonite\SL_A\ART_APL\PG_Apollo\P_Bender'),
(12194, 'PG_Edison', '\Kryptonite\SL_A\ART_APL\PG_Apollo\P_Bender\test\PG_Edison')

SELECT PVT.AreaSK
     , PVT.AreaName
     , PVT.TeamProject
     , PVT.AL
     , PVT.ART
     , PVT.PG
     , PVT.P
     , PVT.T
FROM
(
    SELECT
          T.AreaSK
        , T.AreaName
        , CASE
            WHEN rn = 1 THEN 'TeamProject'
            WHEN rn = 2 THEN 'AL'
            WHEN rn = 3 THEN 'ART'
            WHEN rn = 4 THEN 'PG'
            WHEN rn = 5 THEN 'P'
            WHEN rn = 6 THEN 'T'
          END 'Headings'
        , [Value]
    FROM 
    (
        SELECT T.AreaSK
             , T.AreaName
             , T.AreaPath
             , AreaPath2 = RIGHT(T.AreaPath, LEN(T.AreaPath) - 1) --remove first '\'
        FROM @t T
    )   T
    CROSS APPLY
        dbo.FN_SplitString_AB(T.AreaPath2, @Delim)
) T
PIVOT
(
    MAX(Value)
    FOR Headings IN ([T],[P],[PG],[ART],[AL],[TeamProject])
) PVT

Output

enter image description here

Mazhar
  • 3,797
  • 1
  • 12
  • 29
  • Thanks!I have managed to do it using the TOKEN function in SSIS, but I will try your suggestions as well! – Lena Meer Jan 11 '18 at 13:15