I am looking to stack multiple substrings in one column. I am looking for alternatives to UNION ALL because my real data is massive and there are over 30 occurrences of sub strings for each row.
Below code parse out the substrings for me but create columns for each sub string. I am looking to stack those smp and smp_Num under one column. I tried using inner join and unpivot but it didn't work for me. I am using DB2.
SELECT Prod_Typ,
SUBSTR(Product, 1, 3) as Smp,
SUBSTR(Product, 4, 4) as Smp_Num,
SUBSTR(Product, 8, 3) as Smp1,
SUBSTR(Product, 11, 4) as Smp_Num1,
SUBSTR(Product, 15, 3) as Smp2,
SUBSTR(Product, 18, 4) as Smp_Num2,
SUBSTR(Product, 22, 3) as Smp3,
SUBSTR(Product, 25, 4) as Smp_Num3,
FROM my_table
My data looks like this.
| Prod_Typ | Product |
|---|---|
| 11789 | AAD3241HHA3261UUB MNN4567 |
| 11790 | TTN7689KJI FSS9980 |
What I am looking for my data to look like:
| Prod_Typ | Smp | Smp_Num |
|---|---|---|
| 11789 | AAD | 3241 |
| 11789 | HHA | 3261 |
| 11789 | UUB | |
| 11789 | MNN | 4567 |
| 11790 | TTN | 7689 |
| 11790 | KJI | |
| 11790 | FSS | 9980 |