Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to parse values after specific text to multiple rows in SQL

I have a table like below in my SQL server database. I want to parse the numbers from "objectId": and save it as multiple rows. Is this possible? Please help to do it in SQL.

enter image description here

I dont know how to achieve this in SQL.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ID Key Services
14005 ABC-472 [{"workspaceId": "xxxxxxxxxxxxxxxxxxxxxxxxx", "id": "xxxxxxxxxxxxxxxxxxxxxxxxx:32583","objectId": "32583"},{"workspaceId": "xxxxxxxxxxxxxxxxxxxxxxxxx","id": "xxxxxxxxxxxxxxxxxxxxxxxxx:8965","objectId": "8965"}]

>Solution :

Try this:

DROP TABLE IF EXISTS #tempTable;

CREATE TABLE #tempTable (
    ID int,
    [Key] varchar(25),
    Services varchar(max)
);

INSERT INTO #tempTable (ID, [Key], Services)
VALUES (14005, 'ABC-472', '[{"workspaceId": "xxxxxxxxxxxxxxxxxxxxxxxxx", "id": "xxxxxxxxxxxxxxxxxxxxxxxxx:32583", "objectId": "32583"}, {"workspaceId": "xxxxxxxxxxxxxxxxxxxxxxxxx", "id": "xxxxxxxxxxxxxxxxxxxxxxxxx:8965", "objectId": "8965"}]');

SELECT *
FROM #tempTable;


SELECT ID, [Key], workspaceId, requestId, objectId
FROM #tempTable
CROSS APPLY OPENJSON(Services)
WITH 
(
    workspaceId varchar(50) '$.workspaceId',
    requestId varchar(50) '$.id',
    objectId varchar(50) '$.objectId'
) as ServicesJson;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading