环境:SQL Server 2014及以上
如何使用 SELECT 语句访问 JSON 值中的电子邮件值?
select JSON_VALUE('[{"data":{"email":"test@email.com"}}]', '$.email') as test
Run Code Online (Sandbox Code Playgroud)
Json 支持仅在 SQL Server 2016 中引入 - 因此对于任何早期版本,您需要使用字符串操作代码或简单地在 SQL Server 外部解析 json(可能使用 CLR 函数)
对于 2016 版本或更高版本,您可以JSON_VALUE这样使用:
declare @json as varchar(100) = '[{"data":{"email":"test@email.com"}}]';
select JSON_VALUE(@json, '$[0].data.email') as test
Run Code Online (Sandbox Code Playgroud)
对于旧版本 - 您可能可以摆脱这个问题,但如果您的 json 值不包含属性email,您将得到意想不到的结果:
select substring(string, start, charindex('"', string, start+1) - start) as test
from (
select @json as string, charindex('"email":"', @json) + 9 as start
) s
Run Code Online (Sandbox Code Playgroud)
您可以在以下位置观看现场演示db<>fiddle
| 归档时间: |
|
| 查看次数: |
2538 次 |
| 最近记录: |