2

The below JSON data is one of the field in the table. In the below JSON data, I need to replace the value which have "Not available" in the expLevel into "Not listed" using SQL query.

"Information": {
        "Name": [],
        "Class": [],
        "Degree": ["Graduate or professional degree"],
        "major": [],
        "skill": [],
        "expLevel": ["0 to 2 years",
                     "Not available",
                     "3 to 5 years"],
        "certificationtype": ""
    }

I have tried this:

update sr set filter = replace(filter, '"Not available"', '"available" , "listed"')
from sharedreports_check sr 
where filter like '%"expLevel":[[]"%Not available%"%'

But it is not worked.

Please let me know what will be the SQL query to replace it.

1
  • 1
    FYI , Now Json is supported in sql server 2016.Now we can query on json type just as we query in xml type. Commented Apr 28, 2016 at 8:13

2 Answers 2

2

Try this

update sr set filter = replace(filter, 'Not available', 'Not listed')
from sharedreports_check sr 
where filter like '%expLevel%Not available%'
Sign up to request clarification or add additional context in comments.

Comments

0

If you can manipulate this json in front end then it is better.

Another option is to create CLR,if it is very frequent work and data get populated from other source .

Try this script,i think it will work.(for test test try few more example). Also there is no need of where clause.

declare @i nvarchar(max)='"Information": {
        "Name": [],
        "Class": [],
        "Degree": ["Graduate or professional degree"],
        "major": [],
        "skill": [],
        "expLevel": ["0 to 2 years",
                     "Not available",
                     "3 to 5 years"],
        "certificationtype": ""
    }'

-- 1st method
select replace(@i, 'Not available', 'Not listed')

-- 2nd method which is more accurate
select replace(substring(col1,0,charindex(']',col1)),'Not available', 'Not listed') from
(select substring (@i,charindex('expLevel"',@i)+len('expLevel"')+1,len(@i)) Col1)t4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.