在MS SQL中过滤数据

时间:2018-07-20 11:54:41

标签: json sql-server database tsql sql-server-2016

您好,我在数据库中有大量数据,因此我需要一种方法来过滤sql数据以及其中的特定数据。

例如:我想从此代码中获取姓名和文字

INSERT INTO `Employers` VALUES 
('rgthtr5','9123123','{"name":"Mohamed","text":"employer","bla":"
bla","bla":"bla"})

1 个答案:

答案 0 :(得分:1)

您可以使用JSON_VALUE从json获取值:

declare @tmp table ([code] varchar(10), [ncode] varchar(10), [json] varchar(max))

INSERT INTO @tmp VALUES 
 ('rgthtr5', '9123123', '{"name":"Mohamed1","text":"employer1","bla":"bla","bla":"bla"})')
,('rgthtr6', '9123124', '{"name":"Mohamed2","text":"employer2","bla":"bla","bla":"bla"})')
,('rgthtr7', '9123125', '{"name":"Mohamed3","text":"employer3","bla":"bla","bla":"bla"})')

select 
    [code], 
    [ncode], 
    JSON_VALUE([json], '$.name') as [name], 
    JSON_VALUE([json], '$.text') as [text]
from @tmp

结果:

enter image description here