sqljsonsql-serverwhere-clausesql-server-json

SQL Server where Clause on JSON Field


I have a table

ID |  Start Date       | End Date          | Summary
---+-------------------+-------------------+----------------------------
1  | 2020-01-01T09:20  | 2020-01-01T09:30  | {"total":20,"totalError":10}
1  | 2020-01-02T09:20  | 2020-01-02T10:55  | {"total":10,"totalError":5}

I want to query where totalError > 0

Select * 
from runLog 
where Summary.totalError > 0

Is this possible?


Solution

  • Use JSON_VALUE():

    select * from runLog where json_value(summary, '$.totalError') > 0