Michael Wood
Michael Wood

Reputation: 25

create a new column within a select statement

Heres the code im using.

 select address, area,
 case area
 when area < 1500 then 'small'
 when area > 2500 then 'large'
 else 'medium'
 end as size
 from listings
 where zip = 95677;

With this i want to create a new column that will tell you whether a house is big or small depending on the area size.

This is the error i get and im not really sure how to cast it to make it work. If that is the solution to it like the hint says.

LINE 3: when area < 1500 then 'small' ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

Upvotes: 0

Views: 68

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269693

The correct syntax is:

 select address, area,
        (case when area < 1500 then 'small'
              when area > 2500 then 'large'
              else 'medium'
         end) as size
 from listings
 where zip = 95677;

case has two forms. The above is the case when <condition> form, where the when clauses are fully formed conditions. The other form is more like a switch statement in C: case area when 10 then 'ten' when 20 then 'twenty' . . .. This form can only test for equality of a single expression.

Upvotes: 1

Related Questions