..if this helps for any reason .. GREAT .. depends on Data
i am assuming always will be KDL ... based on this
declare @checkRegionCountryModel nvarchar(100) = 'Europe : Belgium : KDL345,KDL900 ; Europe : Czech Republic : KDL345,KDL900 ; Asia : India :KDL345'
; with cte as
(
select ROW_NUMBER() over(order by value) as rn , replace(replace(value, ':KD','#KD'),': KD','#KD') as ok from string_split(@checkRegionCountryModel ,';')
) , cte_1 as
(
select rn,row_number() over(partition by rn order by rn) as sam, value as bhim from cte a cross apply string_split ( a.ok , '#') b
) , cte_2 as
(
select a.rn,value from cte_1 a cross apply string_split (a.bhim,',') b
where a.sam <> 1
)
select a.bhim,b.value from cte_1 a join cte_2 b on a.rn = b.rn where a.sam = 1
DECLARE @checkRegionCountryModel nvarchar(100) = 'Europe : Belgium : KDL345,KDL900 ; Europe : Czech Republic : KDL345,KDL900 ; Asia : India :KDL345,KDL347';
if i add one more model it is not returning me entire model name in the last row below is the screenshot for reference chances of coming more region , country and model in the input.
DECLARE @checkRegionCountryModel nvarchar(100) = 'Europe : Belgium : KDL345,KDL900 ; Europe : Czech Republic : KDL345,KDL900 ; Asia : India :KDL345,KDL347';
; with
cte as
(
select ROW_NUMBER() over(order by value) as rn , * from string_split(@checkRegionCountryModel ,';')
),
cte_1 as
(
select value, LEFT(value, CHARINDEX ( ':' , value , CHARINDEX ( ':' , value , 0) +1 ) ) as value123 from cte e
union all
select value ,RIGHT(value, len(value)- CHARINDEX ( ':' , value , CHARINDEX ( ':' , value , 0) +1 )) as value123
from cte
) ,
cte_rn as
(
select ROW_NUMBER () over(partition by value order by value123) as rn , * from cte_1
) ,
cte_next as
(
select *,value as joinvalue from cte_rn where rn <> 1
) , cte_ok as
(
select rn, b.value , a.joinvalue from cte_next a cross apply string_split( a.value123 ,',') b
)
select b.value123, a.value from cte_ok a join (select * from cte_rn where rn = 1 ) b on a.joinvalue = b.value
go
SELECT
region,
country,
LTRIM(RTRIM(split3.Item)) AS model
FROM (
SELECT
LTRIM(RTRIM(MAX(CASE WHEN split2.ItemNumber = 1 THEN split2.Item ELSE '' END))) AS region,
LTRIM(RTRIM(MAX(CASE WHEN split2.ItemNumber = 2 THEN split2.Item ELSE '' END))) AS country,
LTRIM(RTRIM(MAX(CASE WHEN split2.ItemNumber = 3 THEN split2.Item ELSE '' END))) AS model
FROM dbo.DelimitedSplit8K(@checkRegionCountryModel,';') AS split1
CROSS APPLY dbo.DelimitedSplit8K(split1.Item, ':') AS split2
GROUP BY split1.ItemNumber
) AS split2
CROSS APPLY dbo.DelimitedSplit8K(model, ',') AS split3
ORDER BY region, country, model
My initial query was incomplete at first. I've now completed it. Please see if it fully meets your needs now:
SELECT
region,
country,
LTRIM(RTRIM(split3.Item)) AS model
FROM (
SELECT
LTRIM(RTRIM(MAX(CASE WHEN split2.ItemNumber = 1 THEN split2.Item ELSE '' END))) AS region,
LTRIM(RTRIM(MAX(CASE WHEN split2.ItemNumber = 2 THEN split2.Item ELSE '' END))) AS country,
LTRIM(RTRIM(MAX(CASE WHEN split2.ItemNumber = 3 THEN split2.Item ELSE '' END))) AS model
FROM dbo.DelimitedSplit8K(@checkRegionCountryModel,';') AS split1
CROSS APPLY dbo.DelimitedSplit8K(split1.Item, ':') AS split2
GROUP BY split1.ItemNumber
) AS split2
CROSS APPLY dbo.DelimitedSplit8K(model, ',') AS split3
ORDER BY region, country, model