0

I have a query like this :

Select office_name, ofc_code 
from table1 
where ofc_code in (select ofc_institution from table2 where id ='2')

If i run this i get an error:

Operator does not exist:numeric = character varying
Hint : no operator matches the given name and argument types

In the above query: ofc_code is numeric and ofc_institution is a character varying

1 Answer 1

1

Don't compare apples to oranges. '2' is a string value, 2 is a number.

You also need to convert ofc_institution to a number if you are certain that it only contains numbers (the question then is: why on earth are you storing numbers in a varchar column?):

Select office_name, ofc_code 
from table1 
where ofc_code in (select ofc_institution::numeric
                   from table2 
                   where id = 2);

If you can't be certain that ofc_institution is always a number, then cast the ofc_code to a string - but that then begs the question why are you comparing those columns to begin with:

Select office_name, ofc_code 
from table1 
where ofc_code::varchar in (select ofc_institution
                            from table2 
                            where id = 2);
Sign up to request clarification or add additional context in comments.

4 Comments

@StevieWeedyLyngdoh: see my edit. You seem to have a horrible table design if you are comparing apples with oranges
Yeah i know ..actually the query was written by someone else...i came to fixed those errors... thats y im having a lots of errors in those queries... and i have to do it..its my job... neway thank you for your help..really appreciate it..thanks
Before properly fixing the queries, please be sure to look at the table structure and understand, that relational databases are not dynamically typed. If you have a psql session, just do a \d+ table1 and \d+ table2 and look at the data types of columns table1.ofc_code, table2.ofc_institution and table2.id. That should make you understand what is wrong with this query and others.
@a_horse_with_no_name ..yeah i got it..its solved ..thanks a lot once again.... 😊

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.