Hi @Sam ,
SQLServer before 2016 cannot use string_split, your SQLServer version is 2012, so you need to use user-defined functions to achieve.
Please refer to:
create table #test(Sl int, Locations char(15), Ratings char(15))
insert into #test values(132 ,'ABC/DEF/GHE', 'L/M/H'),(332, 'ABC/GHE', 'M/H')
CREATE FUNCTION SplitStr(@Sourcestr VARCHAR(8000), @Seprate VARCHAR(100))
RETURNS @result TABLE(F1 VARCHAR(100))
AS
BEGIN
DECLARE @sql AS VARCHAR(100)
SET @Sourcestr=@Sourcestr+@Seprate
WHILE(@Sourcestr<>'')
BEGIN
SET @sql=left(@Sourcestr,CHARINDEX('/',@Sourcestr,1)-1)
INSERT @result VALUES(@sql)
SET @Sourcestr=STUFF(@Sourcestr,1,CHARINDEX('/',@Sourcestr,1),'')
END
RETURN
END
GO
;with cte
as(SELECT *,row_number() over(partition by Locations order by Locations) r1
FROM #test t
CROSS APPLY SplitStr(t.Locations,'/') V)
,cte2 as(SELECT *, row_number() over(partition by Locations order by Locations) r2
FROM #test t
CROSS APPLY SplitStr(t.Ratings,'/') V)
select c1.Sl, c1.Locations,c1.Ratings,c1.F1,c2.F1 from cte c1
join cte2 c2 on c1.Sl=c2.Sl and c1.r1=c2.r2
drop table #test
drop function SplitStr
Output:
Sl Locations Ratings F1 F1
132 ABC/DEF/GHE L/M/H ABC L
132 ABC/DEF/GHE L/M/H DEF M
132 ABC/DEF/GHE L/M/H GHE H
332 ABC/GHE M/H ABC M
332 ABC/GHE M/H GHE H
Regards
Echo
If you have any question, please feel free to let me know.
If the response is helpful, please click "Accept Answer" and upvote it.
Regards
Echo
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Hot issues November--What can I do if my transaction log is full?
Hot issues November--How to convert Profiler trace into a SQL Server table
No, some time it is up to 5 or 6,
Do you have any update?
Please also remember to mark the replies as answers if they helped.
Your action would be helpful to other users who encounter the same issue and read this thread.
Echo
Do you have any update?
Echo
Sign in to comment