在这里,我无法在 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/