PostgreSql +布尔的Typecast到字符类型

时间:2010-10-14 12:12:53

标签: database postgresql sql

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

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 '%%'进行类型转换 建议我如何进行类型转换?

1 个答案:

答案 0 :(得分:6)

如何使用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”。