You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PostgreSQL查询报错:IS TRUE参数需为布尔类型而非文本类型

PostgreSQL JSON布尔值检查报错的解决方法

你的查询语句如下:

WITH cte AS (
   SELECT b.message::json AS response,
    a.quote_reference AS quote_reference,
    b.message AS message_text
            FROM quote_table a
            JOIN messages_table b on a.quote_id = b.quote_id
                WHERE
                b.service = 'MYSQRVICE'
                AND b.call_type = 'RESPONSE'
                AND a.transaction_timestamp >= '2024-06-01'
    ORDER BY b.date_created desc
    )
SELECT
    CASE WHEN response -> 'detailed' -> 'full' ->> 'hibCodeUsed' is true
        THEN 'False'
    ELSE
        'True'
    END AS hibCodeUsed,
    quote_reference,
    message_text
FROM cte;

执行时触发报错:

ERROR: argument of IS TRUE must be type boolean, not type text
LINE 16: CASE WHEN response -> 'comprehensive' -> 'full' -...
^
SQL state: 42804
Character: 608

问题原因

->>运算符提取JSON字段后返回文本类型,但IS TRUE判断要求操作数是布尔类型,因此出现类型不匹配错误。同时注意报错中提到的'comprehensive'和查询里的'detailed'字段名不一致,需确认是否为拼写错误。

解决方法

有两种可行的修正方式:

方法1:直接提取JSON布尔值

使用->运算符(返回JSON类型),PostgreSQL会自动将JSON布尔值解析为SQL布尔值用于判断:

CASE WHEN response -> 'detailed' -> 'full' -> 'hibCodeUsed' IS TRUE
    THEN 'False'
ELSE
    'True'
END AS hibCodeUsed

方法2:显式转换文本为布尔类型

如果JSON中的hibCodeUsed是字符串形式的"true"或"false",可以将提取的文本显式转换为布尔类型:

CASE WHEN (response -> 'detailed' -> 'full' ->> 'hibCodeUsed')::boolean IS TRUE
    THEN 'False'
ELSE
    'True'
END AS hibCodeUsed

内容的提问来源于stack exchange,提问作者runnerpaul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 22:35:10