【发布时间】:2020-05-28 01:13:59
【问题描述】:
我在 SQL Server 数据库中有一个表,它在其中一个列中存储 JSON。结构如下:
桌人
| Name | ExtraInfo |
|--------|:------------------------------------------:|
| Bob | {"Age":"23","Colors":["Red","Blue"]} |
| James | {"Age":"26","Colors":["Green","Yellow"]} |
如果我运行这个查询:
select
Json_value(ExtraInfo,'$.Age') as Age,
json_query(ExtraInfo, '$.Colors') as Colors
from Persons
我会得到这样的东西:
| Age |Colors |
|-----|:-------------------|
| 23 | ["Red","Blue"] |
| 26 | ["Green","Yellow"]|
但是,我需要将 JSON 数组的 Colors 属性转换为 nvarchar 列,其中数组的所有值由空格字符连接,如下所示:
| Age | Colors |
|-----|:-------------|
| 23 | Red Blue |
| 26 | Green Yellow |
有什么方法可以组合多个调用json_query 或其他类似方法在单个 SQL 查询中完成此操作?
【问题讨论】:
标签: sql arrays sql-server json-query open-json