Skip to content
Advertisement

SQL syntax to achieve the “opposite” of LIKE

I’m very rusty on the little SQL I once learned, and while I totally get SELECT column1 FROM table WHERE column2 LIKE pattern, what I need is:

SELECT column1 FROM table WHERE pattern_instance LIKE column2  -- legal or not?

For example, I might have a product family table which has information about the Coca Cola range, and for which I might store coca% in column2 ready to match specific instances of products such as Coca Cola 500ml can or Coca Cola 1.5L bottle. Given such a specific product instance, how would I find the product family in this case?

I need to implement a query to do this in PostgreSQL in case their syntax differs from standard SQL.

Edit: The sense in which I mean the “opposite” of LIKE is that reference material gave the WHERE column LIKE pattern form, but a query in which I used WHERE instance LIKE column (and column contained the pattern) failed. Turns out the source of the failure was NOT having the LIKE parameters the opposite way around, but something else entirely…

Advertisement

Answer

You can store the pattern in a column:

WHERE 'Coca Cola' LIKE column2

would match rows where column2 contains 'Coca%', for instance.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement