inet types and LIKE doesn't work as expected

Started by Phil Mayersover 24 years ago2 messagesgeneral
Jump to latest
#1Phil Mayers
p.obfuscate.mayers@ic.ac.uk

Try this:

hdbdev=> create table ips ( ip inet, ip_txt varchar(15) );
hdbdev=> insert into ips (ip,ip_txt) values ('192.168.1.1','192.168.1.1');
hdbdev=> select * from ips where ip like '192.168.1.1';
ip | ip_txt
----+--------
(0 rows)

hdbdev=> select * from ips where ip_txt like '192.168.1.1';
ip | ip_txt
-------------+-------------
192.168.1.1 | 192.168.1.1
(1 row)

That seems inconsistent?

--
Regards,
Phil

+------------------------------------------+
| Phil Mayers                              |
| Network & Infrastructure Group           |
| Information & Communication Technologies |
| Imperial College                         |
+------------------------------------------+
#2Allan Engelhardt
allane@cybaea.com
In reply to: Phil Mayers (#1)
Re: inet types and LIKE doesn't work as expected

Phil Mayers wrote:

Try this:

hdbdev=> create table ips ( ip inet, ip_txt varchar(15) );
hdbdev=> insert into ips (ip,ip_txt) values ('192.168.1.1','192.168.1.1');
hdbdev=> select * from ips where ip like '192.168.1.1';
ip | ip_txt
----+--------
(0 rows)

hdbdev=> select * from ips where ip_txt like '192.168.1.1';
ip | ip_txt
-------------+-------------
192.168.1.1 | 192.168.1.1
(1 row)

That seems inconsistent?

The default text conversion for the inet type includes a mask:

test=# select text(ip) from ips;
text
----------------
192.168.1.1/32
(1 row)

so

select * from ips where ip like '192.168.1.1%';

works as expected.

The joys of semanically rich data types :-)

Hope this helps a little.

Allan.