database - PostgreSql + 将 bool 值类型转换为字符类型

标签 database postgresql sql

在这里,我无法在 postgre sql 查询中将 bool 值转换为字符。

SELECT *FROM ltl_class_nmfc_aliases 
WHERE ltl_class_nmfc_aliases.id 
NOT IN(SELECT ltl_class_nmfc_aliases_id FROM commodities_shippeds 
WHERE commodities_shipped_obj_type LIKE 'ClientOffice') 
OR ltl_class_id IS NULL 
AND lower(commodity_description_alias) LIKE E'%%' 
AND lower(ltl_value) LIKE E'%92.5%' 
AND hazardous :: integer LIKE E  '%%' 
AND cast(schedule_b as character varying(255)) LIKE E'%%' 
AND cast(harmonized_tariff as character varying(255)) LIKE E'%%' 
ORDER BY commodity_description_alias,ltl_value LIMIT 25;

这里我无法在 AND hazardous::integer LIKE E '%%' 处进行类型转换 建议我如何进行类型转换?

最佳答案

使用 case 语句怎么样?

(case when hazardous then 'Bad juju' else 'Ok.' end) as safety

你也可以使用cast语句:

postgres=# select cast(1 as boolean);
 bool
------
 t

postgres=# select cast(0 as boolean);
 bool
------
 f

postgres=# select cast('false' as boolean);
 bool
------
 f

postgres=# select cast('False' as boolean);
 bool
------
 f

postgres=# select cast('T' as boolean);
 bool
------
 t

postgres=# select cast('F' as boolean);
 bool
------
 f

postgres=# select cast('Y' as boolean);
 bool
------
 t

postgres=# select cast('N' as boolean);
 bool
------
 f

可能是:

 select ... where ... and hazardous = cast(:your_variable as bool)

您也可以转换为 varchar:

select cast(hazardous to varchar)...

一些数据库驱动程序实现(如 Borland 的 BDE)对此感到窒息,因为他们期望 varchar 字段有明确的列宽。

如果您只是过滤 hazardous=true,请仅使用“AND hazardous”。

关于database - PostgreSql + 将 bool 值类型转换为字符类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3932932/

相关文章:

mysql - 查询表中非唯一元素的数量

ruby-on-rails - Heroku Postgres Dump 不创建序列

mysql - 在一条 MySQL 语句中运行多个查询来获取行数

linux - Postgres 错误 : database “test” is being accessed by other users

MySQL 多列匹配多行结果

postgresql - 如何优化 PostgreSQL 中时间戳的连接

php - 如何防止 PHP 中的 SQL 注入(inject)?

sql - 使用 SQL STUFF 连接我无法分组的行

mysql - 如果第一个字母相同,SQL Access 内连接

postgresql - 为什么这个查询会死锁?