从SQL Server中的Json检索值

时间:2016-07-04 09:26:39

标签: sql sql-server json

我有一个表,其中包含一个带有json Data

的列
[
{"Name":"Territory","Value":"1000000002"},{"Name":"Village","Value":"Bahaibalpur"},
{"Name":"Activity Date","Value":"2016-6-15 5:30:0"},
{"Name":"Start Time","Value":"16:37"},
{"Name":"End Time","Value":"17:38"},
{"Name":"Duration","Value":"1hrs 1 mins"}
]

。我想在函数内获取开始时间值和结束时间值。

2 个答案:

答案 0 :(得分:1)

试试这种方式

declare @t table(col varchar(1000))
insert into @t(col)
select  '[{"Name":"Territory","Value":"1000000002"},{"Name":"Village","Value":"Bahaibalpur"},
{"Name":"Activity Date","Value":"2016-6-15 5:30:0"},
{"Name":"Start Time","Value":"16:37"},
{"Name":"End Time","Value":"17:38"},
{"Name":"Duration","Value":"1hrs 1 mins"}]'

select 
substring(col,charindex('Start Time',col)+21,5)  as start_time,
substring(col,charindex('end Time',col)+19,5)  as end_time
from @t

答案 1 :(得分:0)

您没有说明您使用的是哪个版本的SQL,2016内置了功能(https://msdn.microsoft.com/en-us/library/dn921897.aspx

否则,有一篇关于使用TSQL解析Json的文章,你可以阅读here

另一种选择是使用CLR功能。