Matt Inwood
Matt Inwood

Reputation: 137

Comparing text of two fields in PostgreSQL

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> 

Upvotes: 2

Views: 116

Answers (2)

Hambone
Hambone

Reputation: 16397

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

Upvotes: 1

alzee
alzee

Reputation: 1396

You've got your wildcards backwards, you want:

case when dealer_name ilike ('%' || car_make || '%') THEN 'OnBrand'

Upvotes: 2

Related Questions