4

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 ?

1 Answer 1

9

How about using a case statement?

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

You can also use the cast statement:

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)...

Some database driver implementations (like BDE from Borland) choke on this because they expect an explicit column width for varchar fields.

if you are just filtering for hazardous=true, use just "AND hazardous".

Sign up to request clarification or add additional context in comments.

5 Comments

I am trying case at my end but i am not getting any success. I am going to try your code now.
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'%%';
ERROR: argument of AND must be type boolean, not type text
in the where clause use "AND (case when hazardous then 'True' else 'False' end)='True'"
if you are not comparing "hazadous" with something, just filtering for value=true, the correct test is just "AND hazardous"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.