2016-04-28 25 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