I have a variable that contains a json string which is quite long. Datatype therefore is NVARCHAR(MAX)
. Within this json string there are many of the same tags
You can try to create SQL CLR function and using regex expression to match the data you need. Here you can find details of what is SQL CLR integration, examples of regex match function provided by Mircosoft and instructions how to create such functions.
So, having the above setup, you can do something like this:
DECLARE @data NVARCHAR(MAX) = N'
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"IncidentName":"Value1",
"IncidentName":"Value2",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"IncidentName":"Value3",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
},
"IncidentName":"Value4"
}
}
}';
SELECT *
FROM [dbo].[fn_Utils_RegexMatches] (@data, '(?i)(?<="IncidentName":")[^"]+(?=")');
The regex I am using is doing the following:
"
"
Basically, you can use this quick reference to help you build regular expression that is going to work for your case.
The hard part is understanding what SQL CLR is and to implement the Microsoft's String Utility Pack. Then, you can solve issues, which are very difficult or not efficient to solve using plain T-SQL.