views:

19

answers:

1

Here, I am unable to convert boolean value into character in postgre sql query.

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;

Here I am unable to typecast at AND hazardous :: integer LIKE E '%%' Suggest me how to make typecast ?

A: 

What about case?

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

You can use 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

So it could be:

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

You can also cast to varchar:

select cast(hazardous to varchar)...

But some older database connection implementations like BDE choke on this because they expect an explicit column width for varchar fields.

Paulo Scardine
I am trying case at my end but i am not getting any success. I am going to try your code now.
Rahul Patil
SELECT * FROM ltl_class_nmfc_aliases LEFT OUTER JOIN ltl_class_nmfcs ON ltl_class_nmfcs.id=ltl_class_nmfc_aliases.ltl_class_nmfc_id LEFT OUTER JOIN ltl_classes ON ltl_classes.id=ltl_class_nmfcs.ltl_class_id 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 case when hazardous then 'True' else 'False' end AND cast(schedule_b as character varying(255)) LIKE E'%%';
Rahul Patil
ERROR: argument of AND must be type boolean, not type text
Rahul Patil
in the where clause use "AND (case when hazardous then 'True' else 'False' end)='True'"
Paulo Scardine
if you are not comparing "hazadous" with something, just filtering for value=true, the correct test is just "AND hazardous"
Paulo Scardine