0

I have a table where it contains data in below format enter image description here

How to achieve this in MS SQL Server.

13
  • Which version of MS SQL Server are you using? Commented Sep 3, 2018 at 9:48
  • 1
    Possible duplicate stackoverflow.com/questions/26293157/… Commented Sep 3, 2018 at 9:50
  • 4
    Possible duplicate of Splitting Comma separated values in columns to multiple rows in Sql Server Commented Sep 3, 2018 at 9:51
  • 1
    What were wrong with the (probably 100's) of other questions with answers that were presented to you when you searched and wrote this question? Commented Sep 3, 2018 at 9:52
  • This is not a duplicate, this is a different question with multiple columns with multiple values, using a bad data model Commented Sep 3, 2018 at 9:54

1 Answer 1

1

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];
Sign up to request clarification or add additional context in comments.

4 Comments

I want to replace comma with semi colon. I have replaced comma with semi colon, but it throws error.:JSON text is not properly formatted. Unexpected character ';' is found at position 42."Do you have any idea about it.
@RamBhaskar what do you mean? There are no commas in the final dataset. Do you mean on your original data it is now separated by a ; instead of a ,?
Yes, Now delimiter is semicolon; I used the query that you had given but in the same statement, I replaced comma with semi colon using replace function.
@RamBhaskar If the delimiter is now a ; then just change the REPLACE from a , to ;. An important part of using the SQL you get from the volunteers here is understanding it; if you don't you shouldn't be using it. Changing the delimiter is the simplest thing you can do here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.