PostgreSql +布尔的Typecast到字符类型

Rub*_*ist 2 sql database postgresql

在这里,我无法在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;
Run Code Online (Sandbox Code Playgroud)

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

Pau*_*ine 7

使用case声明怎么样?

(case when hazardous then 'Bad juju' else 'Ok.' end) as safety
Run Code Online (Sandbox Code Playgroud)

您也可以使用以下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
Run Code Online (Sandbox Code Playgroud)

所以它可能是:

 select ... where ... and hazardous = cast(:your_variable as bool)
Run Code Online (Sandbox Code Playgroud)

你也可以转换为varchar:

select cast(hazardous to varchar)...
Run Code Online (Sandbox Code Playgroud)

一些数据库驱动程序实现(比如来自Borland的BDE)因为期望varchar字段的显式列宽而受阻.

如果你只是过滤hazardous=true,只使用"AND危险".