I am using SQL Server 2016 where I am loading JSON Response in one of the nvarchar(max)
type column.
Now I want to create a derived column or maybe even a view that will find the particular value in that complete JSON Response and display only that in the new derived column.
Example:
**Complete_JSON_Repsone** --> this is SQL column
{"result":{"banner_image_light":"","country":"USA","parent":"","notes":"","stock_symbol":"","u_op_dev_version":"","u_restriciton":"No","discount":"","sys_id":"7a2c008c1b07ac50a62cea0ce54bcbe8","market_cap":"0","customer":"false"}}
I tried the below query, but it's not giving the expected results it prints out everything after sys_id
:
SELECT
Substring (
a.Complete_JSON_Repsone,
Charindex( '"sys_id":', Complete_JSON_Repsone) + 1,
Len(Complete_JSON_Repsone)
) AS [Sys_Idd]
FROM <table-name> a
Current output (actual result):
Sys_Idd
sys_id":"7a2c008c1b07ac50a62cea0ce54bcbe8","market_cap":"0","customer":"false"}}
Expected output:
Sys_Idd
7a2c008c1b07ac50a62cea0ce54bcbe8
UPDATE
Sample Input :
Create table dbo.log1
(
Id varchar(50),
Complete_JSON_Response nvarchar(max),
Sys_Id varchar(50)
)
insert into dbo.log1 (Id,Complete_JSON_Response)
values ('S1','{"result":{"banner_image_light":"","country":"USA","parent":"","notes":"","stock_symbol":"","u_op_dev_version":"","u_restriciton":"No","discount":"","sys_id":"7a2c008c1b07ac50a62cea0ce54bcbe8","market_cap":"0","customer":"false"}}')
,('S2','{"result":{"banner_image_light":"","country":"Aus","parent":"","notes":"","stock_symbol":"","u_op_prod_version":"","u_restriciton":"No","discount":"","sys_id":"5b2c008c1b07ac50a62cea0ce54bcbe8","market_cap":"1","customer":"TRUE"}}')
select * from dbo.log1
Above select query prints, NULL value for Sys_id column as value for that column in not inserted initially. what I want in expected output is that instead of NULL it should populate(derive) only sys_id value from Complete_JSON_Response column to Sys_id column
Expected output:
Id Sys_Id
S1 7a2c008c1b07ac50a62cea0ce54bcbe8
S2 5b2c008c1b07ac50a62cea0ce54bcbe8
JSON_VALUE
should do the trick for you, does it not work?