2016-04-28 282 views
0

下面的JSON数据是表中的一个字段。在下面的JSON数据中,我需要使用SQL查询将expLevel中“Not available”的值替换为“Not listed”。使用SQL查询替换JSON中的数据

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

我已经试过这样:

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

但它不工作。

请让我知道什么是SQL查询来代替它。

+1

仅供参考,现在Json在sql server 2016中支持。现在我们可以查询json类型,就像我们在xml类型中查询一样。 – KumarHarsh

回答

0

试试这个

update sr set filter = replace(filter, 'Not available', 'Not listed') 
from sharedreports_check sr 
where filter like '%expLevel%Not available%' 
0

如果你可以操纵这个JSON在前端则是更好的。

另一个选择是创建CLR,如果它是非常频繁的工作和数据从其他来源填充​​。

试试这个脚本,我认为它会起作用(对于测试测试,请尝试更多的示例)。 也不需要where子句。

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