[英]SQL extracting data from SQL column
I have SQL table JSON data into one of the columns. 我将SQL表JSON数据放入列之一。 The column type is varchar max.
列类型为varchar max。 I have to extract the data from that column using sql.
我必须使用sql从该列中提取数据。 For example
例如
{"RESPONSE":{"value":"<p>this is a test.....</p>","isAnswered":true}}'
I want to extract: this is a test.....
我要提取:
this is a test.....
and get rid of all attributes and Nodes 并摆脱所有属性和节点
I am very new to JSON. 我对JSON非常了解。 Fisrt time looking into it and lost
Fisrt时间调查它并迷路了
You can try this: 您可以尝试以下方法:
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
I'm assuming that the structure of your JSON response is: 我假设您的JSON响应的结构为:
{"RESPONSE":{"value":"","isAnswered":true}} {“ RESPONSE”:{“ value”:“”,“ isAnswered”:true}}
EDIT: I recommend to insert all html tags and names (like ) into the htmlTags table to obtain the result you want since you cannot predict which one of them is going to appear in the json. 编辑:我建议将所有html标记和名称(如)插入htmlTags表中,以获取所需的结果,因为您无法预测其中哪个将出现在json中。
UPDATE: use this set @response = SUBSTRING(@string, PATINDEX('%response%',@string), PATINDEX('%":{"value"%',@string) - PATINDEX('%response%',@string)) so you can replace any kind of RESPONSE pattern in your json. 更新:使用此设置@response = SUBSTRING(@string,PATINDEX('%response%',@ string),PATINDEX('%“:{” value“%',@ string)-PATINDEX('%response%', @string)),因此您可以替换json中的任何一种RESPONSE模式。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.