在 SQL Server 2012 列中查询 JSON [英] Query JSON inside SQL Server 2012 column

查看:43
本文介绍了在 SQL Server 2012 列中查询 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 SQL Server 2012 表中有一个列,其中包含以下 Json 数据.

I have a column inside my SQL Server 2012 table which contains following Json data.

[{"bvin":"145a7170ec1247cfa077257e236fad69","id":"b06f6aa5ecd84be3aab27559daffc3a4"}]

现在我想在我的查询中使用这个列数据

Now I want to use this column data in my query like

select * 
from tb1 
left join tb2 on tb1.(this bvin inside my column) = tb2.bvin.

有没有办法在 SQL Server 2012 中查询 JSON 数据?

Is there a way to query JSON data in SQL Server 2012?

推荐答案

老实说,这是一种糟糕的数据存储架构,可能会导致一些严重的性能问题.

Honestly, this is a terrible architecture for storing the data, and can result in some serious performance issues.

如果您确实无法控制更改数据库,您可以通过像下面这样使用 SUBSTRING 解析出值来实现这一点,但它会导致非常不开心的路径:

If you truly don't have control to change the database, you can accomplish this by parsing out the value with SUBSTRING like below, but it's leading down a very unhappy path:

SELECT *
FROM tb1
JOIN tb2 on tb2.bvin = 
    SUBSTRING(
        tb1.json
        ,CHARINDEX('"bvin":"', tb1.json) + LEN('"bvin":"')
        ,CHARINDEX('"', tb1.json, CHARINDEX('"bvin":"', tb1.json) + LEN('"bvin":"')) - CHARINDEX('"bvin":"', tb1.json) - LEN('"bvin":"')
    )

遗憾的是,这很容易.

这篇关于在 SQL Server 2012 列中查询 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆