I need to extract an ID from some a string field (JSON code). From performance perspective what is the best way to obtain this ID and where?
Remarks:
- there are different type of messages
- payload: JSON field
- sample of message type1: {"RequestId":"4B5E95D49D3E46548904083D0CD17521"}
- sample of message type2:{"createdById":"XXXXXXXXX","requestId":"15F79EC78E5243A487337C0FE61A9E00","sessionId":null}
- SQL in order to extract the id:
select
(Case
when eventType = 'Type1' then substring (payload, CHARINDEX('{',payload)+ LEN('{"RequestId":"'), CHARINDEX ('"}', payload) -( CHARINDEX('{',payload)+ LEN('{"RequestId":"')))
when eventType = ' Type2' then substring (payload, CHARINDEX('"requestId":"',payload)+ LEN('"requestId":"'), CHARINDEX ('","sessionId"', payload) -( CHARINDEX('"requestId":"',payload)+ LEN('"requestId":"')))
else 'none' END) as RequestID
from table
CHARINDEXandSUBSTRINGnot use SQL Server's JSON functionality?@@versionof SQL Server is... - ?