Skip to content
Advertisement

SQL extract total number of instances of a substring in an array in JSON fields

How can I get a SQL Query to go within the JSON of a Column, and find the number of instances of a substring within that entire Column?

I am using SQL Server 2016.

i.e

So

SELECT COUNT(*) FROM FruitPageTable WHERE [PageData] LIKE '%apples%'

gives me 3…i.e the amount of records that have ‘apples’ in it.

But how to I get the count of ‘apples’ in the Url property within the JSON in PageData, across all records? i.e 6 mentions in total. Is there some syntax with JSON_QUERY that I should use?

Advertisement

Answer

I think you don’t need json_query to check number of 'apples', string and mathematical functions will do this.

See dbfiddle

sqlserver, use openjson

see sqlfiddle

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement