2

I'm trying to judge by the name of a car dealership whether a specific car is their brand or not. So with:

select car_make, dealer_name, 
case when ('%' || dealer_name || '%') ilike car_make THEN 'OnBrand'
END
from table

Ideally, when searching a car with make Nissan and a dealer named Local Nissan Dealer, the third column would contain OnBrand, but it's returning

Nissan    Local Nissan Dealer    <null> 

2 Answers 2

2

You've got your wildcards backwards, you want:

case when dealer_name ilike ('%' || car_make || '%') THEN 'OnBrand'
Sign up to request clarification or add additional context in comments.

4 Comments

This also returns a null for that field.
Well the syntax is correct this way, but it's not in the question. Can you provide a complete example row and the table definition (CREATE TABLE statement).
Is it not possible to do this syntax in 8.1? That's the only thing I can think of that might drop your example from working. Both fields are VARCHAR and not null.
I will look. Can you provide the full table definition/schema please? That would be the CREATE TABLE statement. -- The case statement has been available since at least Pg 7.4 If you are not getting an actual error when issuing the query, then it is correct.
1

This doesn't address why your particular statement isn't working (@user's solution looks good to me), but as an alternative, potentially simpler syntax, what about a regular expression?

select
  car_make, dealer_name, 
  case when dealer_name ~ car_make THEN 'OnBrand' end
from table

Comments

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.