在sql server中的一个大字符串中需要一个值

Need one value in a big string in sqlserver

需要Sqlserver查询。

示例 1:

{ "Id": 1785, "Type": 10, "Vendor": "vendor1", "Brand": "brand1", "LOB": 0, "Category": "Supported", "Name": "Security Bulletin", "MarketVersion": "WW17-150", "InternalVersion": "1.0", "Edition": "", "Role": "", "CDIVersion": "", "BuildVersion": "", "Patch": "", "Hotfix": "", "Arch": "x86", "ReleaseDate": "2017-09-29", "Released": 1, "Locale": "en-us", "Tag": "", "ConfigDetails": "", "Description": "Security-Only update for Windows (KB4038779, KB4038786, KB4038793)", "Notes": "Wonderware Security Bulletin WW17-085", "UpdateDate": "2017-09-29", "ExceptionStatement": "dddddd"}

示例 2:

{ "Id": 783, "Type": 10, "Vendor": "vendor1", "Brand": "brand1", "LOB": 0, "Category": "Supported", "Name": "Security Bulletin", "MarketVersion": "WW17-085", "InternalVersion": "1.0", "Edition": "", "Role": "", "CDIVersion": "", "BuildVersion": "", "Patch": "", "Hotfix": "", "Arch": "x86", "ReleaseDate": "2017-09-29", "Released": 1, "Locale": "en-US", "Tag": "", "ConfigDetails": "", "Description": "Security-Only update for Windows (KB4038779, KB4038786, KB4038793)", "Notes": "Note: All Microsoft KB#s listed in the table above are supported with Wonderware products listed under Mainstream Support. *Note: KB4038779 is Security-Only update for Windows 7 SP1 and Windows Server 2008 R2 SP1. *Note: KB4038786 is Security-Only update for Windows Server 2012. *Note: KB4038793 is Security-Only update for Windows 8.1 and Windows Server 2012 R2.", "UpdateDate": "2017-09-29", "ExceptionStatement": "dddddd"}

示例 3:

{ "Id": 1859, "Type": 1, "Vendor": "Microsoft", "Brand": "", "LOB": 0, "Category": "", "Name": "Windows", "MarketVersion": "30", "InternalVersion": "", "Edition": "Embedded Standard", "Role": "", "CDIVersion": "", "BuildVersion": "", "Patch": "SP1", "Hotfix": "", "Arch": "x86", "ReleaseDate": "2017-11-21", "Released": 1, "Locale": "en-us", "Tag": "", "ConfigDetails": "", "Description": "desc", "Notes": "notes", "MsSecurityLink": "", "InformationLink": "", "UpdateDate": "1900-01-01", "ExceptionStatement": ""}

在上面的 json 字符串中有 'Type' 属性。我需要类型的值。例如 1 输出应为 10,在第二个示例中输出也应为 10,在第三个示例中输出应为 1。

请帮忙查询sqlserver

尝试以下方法

DECLARE @json varchar(1000)='{ "Id": 1785, "Type": 10, "Vendor": "vendor1", ...}'

DECLARE @pos1 int=NULLIF(CHARINDEX('"Type": ',@json),0)+LEN('"Type": ')
DECLARE @pos2 int=CHARINDEX(',',@json,@pos1)

SELECT @pos1,@pos2,CAST(SUBSTRING(@json,@pos1,@pos2-@pos1) AS int)

查询中

SELECT pos1,pos2,CAST(SUBSTRING(json,pos1,pos2-pos1) AS int) [Type],json
FROM
  (
    SELECT
      NULLIF(CHARINDEX('"Type": ',json),0)+LEN('"Type": ') pos1,
      CHARINDEX(',',json,NULLIF(CHARINDEX('"Type": ',json),0)+LEN('"Type": ')) pos2,
      json
    FROM
      (
        SELECT '{ "Id": 1785, "Type": 10, "Vendor": "vendor1", ... }' json
        UNION ALL SELECT '{ "Id": 783, "Type": 10, "Vendor": "vendor1", ...}'
        UNION ALL SELECT '{ "Id": 1859, "Type": 1, "Vendor": "Microsoft", ...}'
        UNION ALL SELECT '{"IdLeft": 255, "TypeLeft": 0, "IdRight": 284, "TypeRight": 0, "RelationshipType": 1, "Description": "" }'
      ) q
  ) q