简体   繁体   中英

Extract value from JSON array for use in where clause - SQL Server

I have a SQL Server table which contains two columns; Player (VARCHAR), and Availability (JSON/VARCHAR). The data in this table is similar to the below:

Player         Availability
-------+-------------------------------
John   | { "Availability": [1, 3, 6] }
Yusuf  | { "Availability": [3, 4, 7] }
Ben    | { "Availability": [1, 2, 3] }

What I'm wanting is to write a query which will show me what players are available on a certain day (the numbers in the Availability column represent days).

Something like:

SELECT Player 
FROM TableName 
WHERE JSON_VALUE(Availability, '$.Availability') = 3;

This would return: John, Yusuf, and Ben.

Is this achievable?

you can use OPENJSON :

SELECT t.player, json.value
FROM TableName t
CROSS APPLY OPENJSON(Availability,'$.Availability') json
WHERE json.value = 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM