[英]SQL extracting data from SQL column
我將SQL表JSON數據放入列之一。 列類型為varchar max。 我必須使用sql從該列中提取數據。 例如
{"RESPONSE":{"value":"<p>this is a test.....</p>","isAnswered":true}}'
我要提取: this is a test.....
並擺脫所有屬性和節點
我對JSON非常了解。 Fisrt時間調查它並迷路了
您可以嘗試以下方法:
begin transaction
declare @string varchar(max)
declare @result varchar(max)
declare @response varchar(max)
set @string = '{"RESPONSE":{"value":["d"],"isAnswered":true}}'
set @response = SUBSTRING(@string, PATINDEX('%response%',@string), PATINDEX('%":{"value"%',@string) - PATINDEX('%response%',@string))
print @response
-- for html tags
DECLARE @htmlTags TABLE
(
ID INT IDENTITY(1,1),
htmlTag varchar(50)
);
INSERT INTO @htmlTags
VALUES
('<p>'),
('</p>'),
('<h1>'),
('</h1>'),
('"'),
('{'),
('}'),
(':'),
(','),
('value'),
('isAnswered'),
('true'),
('false'),
(' '),
('['),
(']')
;
SET @result = @string
DECLARE @temp varchar(max) = '';
WHILE @result != @temp
BEGIN
SET @temp = @result;
SELECT
@result = Replace(@result, htmlTag, '')
FROM
@htmlTags
;
END;
set @result = REPLACE(@result, @response, '')
print @result
rollback
我假設您的JSON響應的結構為:
{“ RESPONSE”:{“ value”:“”,“ isAnswered”:true}}
編輯:我建議將所有html標記和名稱(如)插入htmlTags表中,以獲取所需的結果,因為您無法預測其中哪個將出現在json中。
更新:使用此設置@response = SUBSTRING(@string,PATINDEX('%response%',@ string),PATINDEX('%“:{” value“%',@ string)-PATINDEX('%response%', @string)),因此您可以替換json中的任何一種RESPONSE模式。
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.