I want to extract the size dimension from the SKU fields which comes in different formats. I tried SPLIT_PART and CHARINDEX/Substr but not able to get it to work for all different formats.
Example SKU formats -
1.C11544-BLK-2XL|AC|PQRS|Baseball Cap
2.C11544-BLK-S-M|AC|PQRS|Baseball Cap
3. K45602-NA-1|XZ|XYSUA|Pant
Expected Output
2XL
S-M
1
Note- The letters for colour is not consistently 3 letter. Same for Style. Only the format is static - SKU|XX|XXXX and so on.
Please try it on your "Data Set all sorts of variations" to see if this solution works
SELECT
SKU,
SUBSTRING(PreSKU, Dash2 + 1, LEN(PreSKU) - Dash2) AS SizeDimension
FROM
(
SELECT
SKU,
-- Extract the part before the first pipe
SUBSTRING(SKU, 1, CHARINDEX('|', SKU) - 1) AS PreSKU,
-- Find the first dash in the pre-pipe string
CHARINDEX('-', SUBSTRING(SKU, 1, CHARINDEX('|', SKU) - 1)) AS Dash1,
-- Find the second dash using the first dash as starting point
CHARINDEX('-', SUBSTRING(SKU, 1, CHARINDEX('|', SKU) - 1), CHARINDEX('-', SUBSTRING(SKU, 1, CHARINDEX('|', SKU) - 1)) + 1) AS Dash2
FROM #SKUs
) AS Derived;