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.
drop table if exists #Temp
create table #temp (col1 varchar(200))
insert into #Temp
select '1.C11544-BLK-2XL|AC|PQRS|Baseball Cap'
union all
select '2.C11544-BLK-S-M|AC|PQRS|Baseball Cap'
union all
select '3. K45602-NA-1|XZ|XYSUA|Pant'
SELECT
right(value,charindex('-',reverse(value))-1)
FROM
#Temp CROSS APPLY string_split(col1,'|',1)
WHERE
ordinal = 1