Separating a string

  • Hi,

    I have a string that comes into one of my column and I need to create a column for each item.

    It looks something like this but much longer

    {"Id":"1bban5d82-23g4c-430ui-b804-4c3awasgec28","RecordType":20,"CreationTime":"2020-07-07T03:39:55"}

    Thanks

    Astrid

     

  • After adding [] to make this valid JSON, I came up with this

    DROP TABLE IF EXISTS #SomeJSON;

    CREATE TABLE #SomeJSON
    (
    JSONString VARCHAR(MAX) NOT NULL
    );

    INSERT #SomeJSON
    (
    JSONString
    )
    VALUES
    ('[{"Id":"1bban5d82-23g4c-430ui-b804-4c3awasgec28","RecordType":20,"CreationTime":"2020-07-07T03:39:55"}]');

    SELECT sj.JSONString
    ,x.id
    ,x.RecordType
    ,x.CreationTime
    FROM #SomeJSON sj
    CROSS APPLY
    (
    SELECT *
    FROM
    OPENJSON(sj.JSONString)
    WITH
    (
    id VARCHAR(100) 'strict $.Id'
    ,RecordType INT '$.RecordType'
    ,CreationTime DATETIME '$.CreationTime'
    )
    ) x;

    • This reply was modified 3 years, 9 months ago by  Phil Parkin.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • thanks!!!

     

     

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply