如何在JSON数组字段上查询

时间:2018-06-22 13:48:32

标签: sql-server-2016

如何查询json数组字段。

我的sql表字段是个人数据,示例数据在下面

[{"Name":"A","Age":"12"},
{"Name":"B","Age":"22"},
{"Name":"C","Age":"22"}]

因此所需的查询类似于Name = A

1 个答案:

答案 0 :(得分:0)

这应该将JSON设置为可以正常查询的格式:

select 
 SUBSTRING([name], CHARINDEX(':', [name]) + 1, 8000) as [name]
,SUBSTRING([age], CHARINDEX(':', [age]) + 1, 8000) as [name]

from
(select
 SUBSTRING(value, 1, CHARINDEX(',', value) - 1) AS [name]
,SUBSTRING(value, CHARINDEX(':',value)+3,CHARINDEX(',',value)-1) as [age]

from
(select replace(replace(replace(replace(strTest, '[', ''), '}', ''), ']', ''), '"', '') as strMod
from [dbo].[tblTest]
) as a

cross apply string_split(strMod, '{')
where value <> ''
) as b