I have ports_list table with ports column below:
ports | intended_output |
---|---|
"[{\"id\":193,\"name\":\"PORT C\"}]" | PORT C |
"[{\"id\":204,\"name\":\"PORT D\"}]" | PORT D |
"[{\"id\":45,\"name\":\"PORT I\"},{\"id\":193,\"name\":\"PORT C\"},{\"id\":204,\"name\":\"PORT D\"},{\"id\":271,\"name\":\"PORT G\"}]" | PORT I, PORT C, PORT D, PORT G |
I use this method and somehow managed to get result when there is only single port value in a row unfortunately it is unable to cater for multiple port values in a row:
select ports,
REPLACE( substring(ports, CHARINDEX('"name\":\', ports, 1)+10, LEN(ports)-10),
'\"}]"',
''
)
from ports_list
ports | output |
---|---|
"[{\"id\":193,\"name\":\"PORT C\"}]" | PORT C |
"[{\"id\":204,\"name\":\"PORT D\"}]" | PORT D |
"[{\"id\":45,\"name\":\"PORT I\"},{\"id\":193,\"name\":\"PORT C\"},{\"id\":204,\"name\":\"PORT D\"},{\"id\":271,\"name\":\"PORT G\"}]" | PORT I"},{"id":193,"name":"PORT C"},{"id":204,"name":"PORT D"},{"id":271,"name":"PORT G"}]" |
Can someone help me to get the intended output as per the first table?