单个单元格将包含如下字符串:
[{"Code":"C015","Index":0,"Message":"blah blah blah","FailSeverity":2},{"Code":"W012","Index":0,"Message":"blah blah blah","FailSeverity":1}]
我有兴趣从上面的单个字符串中提取所有代码。 I.E.我想要" C015"和" W012"从上面的例子中没有别的。
我已经看过Substring()函数,但我不太清楚如何在这种情况下实现它,或者我完全理解它是一个函数。
答案 0 :(得分:0)
对于这样的事情,我认为你想要使用一个基于理货的字符串分割器功能。在这种情况下,我使用Jeff Moden的DelimitedSplit8K
DECLARE @String VARCHAR(8000) = '[{"Code":"C015","Index":0,"Message":"blah blah blah","FailSeverity":2},{"Code":"W012","Index":0,"Message":"blah blah blah","FailSeverity":1}]';
WITH
cte_GetCodes AS (
SELECT
CodeValue = CASE WHEN LAG(dsk.Item, 2) OVER (ORDER BY dsk.ItemNumber) = 'Code' THEN dsk.Item END
FROM
dbo.DelimitedSplit8K(@String, '"') dsk
)
SELECT
*
FROM
cte_GetCodes gc
WHERE
gc.CodeValue IS NOT NULL;
结果...
CodeValue
----------
C015
W012