gpt4 book ai didi

postgresql - 在 PostgreSQL 中将 smallint 转换为 boolean

转载 作者:行者123 更新时间:2023-11-29 11:19:04 24 4
gpt4 key购买 nike

我正在尝试将 smallint 转换为 PostgreSQL 中的 bool 值。这不是开箱即用的,例如:

select (1::smallint)::bool;

返回“错误:42846:无法将类型 smallint 转换为 bool 值”

我可以使用以下方法解决这个问题:

select (1::smallint)::int::bool;

但我想知道是否有一种方法可以定义如何将 smallint 直接转换为 boolean

这样做的原因是我(以及与我一起工作的其他人)有汇总查询,将数据库表中的 int 列转换为 boolean。我想将此列更改为 smallint,但这样做会破坏此逻辑,因为没有从 smallintboolean 的直接转换。是否可以使用 postgres CREATE CAST 来定义如何将 smallint 转换为 boolean

最佳答案

CREATE OR REPLACE FUNCTION boolean1(i smallint) RETURNS boolean AS $$
BEGIN
RETURN (i::smallint)::int::bool;
END;
$$ LANGUAGE plpgsql;

CREATE CAST (smallint AS boolean) WITH FUNCTION boolean1(smallint) AS ASSIGNMENT;

关于postgresql - 在 PostgreSQL 中将 smallint 转换为 boolean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31343809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com