篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql MS SQL JSON_VALUE相关的知识,希望对你有一定的参考价值。
DECLARE @browsers NVARCHAR(1000)
= N'
{ "Browsers": [
{
"Name": "Firefox",
"Info": { "Version": "1.0.1", "Category": "Major" },
"LogDate": 2003, "AddedOn": "2004-11-09" \
},
{
"Name": "Opera",
"Info": { "Version": "1.0.5", "Category": "Dev" },
"LogDate": 2004, "AddedOn": "2008-11-09"
}]
}';
-- Name of the first item
SELECT JSON_VALUE(@browsers, '$.Browsers[0].Name');
-- Output: Firefox
-- Return the Added On Date of the first browser in our array
-- and convert it into a DateTime2 datatype
SELECT CAST(JSON_VALUE(@browsers,
'$.Browsers[0].AddedOn') AS DATETIME2);
-- Output: 2004-11-09 00:00:00.0000000
-- This returns NULL because the 'Browsers' is
-- an array instead of a simple object
SELECT JSON_VALUE(@browsers, '$.Browsers');
-- Output: NULL
-- This is also invalid because JSON_VALUE cannot return
-- an array...only scalar values allowed!
SELECT JSON_VALUE(@browsers, '$.Browsers[1].Info');
-- Output: NULL
-- Much better
SELECT JSON_VALUE(@browsers, '$.Browsers[1].Info.Category');
-- Output: Dev
以上是关于sql MS SQL JSON_VALUE的主要内容,如果未能解决你的问题,请参考以下文章