1

I have a sql table (version 9.3 PostgreSQL) with fields (tour character varying, tourtime int)... I want to write a query like case when tour = 'A' then tourtime = 2 when tour = 'B' then tourtime = 3 else 0 etc..

However i get the error

CASE types integer and boolean cannot be matched

Why is this?

1
  • Cast boolean as integer? tourtime::int4 Commented Jan 5, 2016 at 6:34

1 Answer 1

4

In your CASE you have tree branches:

 case when tour = 'A' 
        then tourtime = 2 
      when tour = 'B' 
        then tourtime = 3 
      else 0
 end

In the first two, you have boolean expressions ((tourtime = 2) and (tourtime = 3)), which are true or false according to the value of the column tourtime. In the third one, you have an integer expression (0).

When the system tries to infer the type of the result it matches the three types and found that they are different.

So you should have either all integer expressions, or boolean expressions, depending on what you want as result.

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

1 Comment

You are missing END in your case.

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.