How to use LIKE on inet field in Postgres

Aley picture Aley · Apr 24, 2013 · Viewed 10.4k times · Source

I need to perform the following query:

SELECT * FROM users WHERE ip LIKE '88.99.%';

The problem is that the inet datatype does not seems to support wildcards (at least not in the way I have utilized them in the preceding query).

What I'd like to know is whether it would be a smart move to convert the field into string before specifying the LIKE filter? (On the other hand, this would probably slow down the whole query, even if the approach itself was valid.)

Answer

Jakub Kania picture Jakub Kania · Apr 24, 2013

Use subnet operators for such queries. '88.99.0.0/16' should do it :

SELECT * FROM users WHERE ip << inet '88.99.0.0/16';