I have several columns in my database. In one of these columns is a JSON string like: fyvfvv.
[
{
"Value": "0000138010687",
"Name": "Code"
}
]
I need to select those rows from the database, whose JSON is filled with the value of the mask "0000****"
, or the value is greater than a certain number.
Can you tell me how to do it in the best way?
2
Answers
You need to parse the stored JSON with
OPENJSON()
and use the appropriateWHERE
clause.Data:
Statement (to get the rows):
Statement (to get the rows and the parsed JSON):
Both statements return the correct results, even if the parsed JSON array contains more than one item.
Assuming the json array contains one element only, then we can use
JSON_VALUE()
to get the element value by path :Demo here