I have a table where it contains data in below format
How to achieve this in MS SQL Server.
This uses DelimitedSplit8K
, as information on the ordinal position is required (something STRING_SPLIT
and many other splitters don't supply). The below is Pseudo SQL as well, as the OP has provided images, rather that textual data:
SELECT {YourColumns}
FROM YourTable YT
CROSS APPLY dbo.DelimitedSplit8K(YT.Qualification,',') DSq
CROSS APPLY dbo.DelimitedSplit8K(YT.Instituion,',') DSi
WHERE DSq.ItemNumber = DSi.ItemNumber;
The true answer here, as has been mentioned in the comments, however, is to fix the data model.
An alternative method would be to use OPENJSON
. This is something I have only been introduced to recently, and I don't have access to a SQL Server 2016 instance to test this against (I have used SQL Fiddle to test it runs though, but not against the image provided for my same reason above). I beleive this should also achieve your goal though:
SELECT OJq.[value], OJi.[Value]
FROM YourTable YT
CROSS APPLY (SELECT ca.[Key], ca.[value]
FROM OPENJSON('["' + REPLACE(YT.Qualification,',','","') + '"]') ca) OJq
CROSS APPLY (SELECT ca.[Key], ca.[value]
FROM OPENJSON('["' + REPLACE(YT.Instituion,',','","') + '"]') ca) OJi
WHERE OJq.[Key] = OJi.[Key];