How to cast text to cidr/inet - Mailing list pgsql-sql

From Sondaar Roelof
Subject How to cast text to cidr/inet
Date
Msg-id F28A2B83DFE0D411A7B3006097487147468FDB@sv7007.scania.se
Whole thread Raw
List pgsql-sql
Hello,

I am battleling around to get a text field into a cidr and inet type field.
The documentation says things like CAST(val AS text) but inet and cidr does
not seem to exist.

However the following works:
dhcp=# insert into dhcp_subnet(ipaddress) values('139.122.172/18');
INSERT 46172 1
dhcp=# select * from dhcp_subnet;id |   ipaddress    | description
----+----------------+------------- 2 | 139.122.128/18 |
(1 row)


But the copying from table to table (text to cidr) does not work.

Perhaps someone can give me some clues.

Additional information (tables, queries, results are below).

Best regards,            Roelof Sondaar            WM-data Zwolle B.V.            Russenweg 5            P O Box 391
       8000 AJ  ZWOLLE            The Netherlands            *  roelof.sondaar@wmdata.nl            * +31 (0) 384 977
366

*******************************************************
Disclaimer

This email is confidential and intended solely for the use of
the individual to whom it is addressed. Any views or opinions
presented are solely those of the author and do not
necessarily represent those of WM-data Zwolle B.V.
If you are not the intended recipient, be advised that you
have received this email in error and that any use,
dissemination, forwarding, printing or copying of this emailis strictly prohibited.

If you have received this email in error please notify
WM-data Zwolle B.V.  Helpdesk by telephone on
+31(0)384 977 319
*******************************************************


Some additional information:
dhcp=# \d dhcp_subnet                            Table "dhcp_subnet" Attribute  |  Type   |
Modifier
-------------+---------+----------------------------------------------------
--id          | integer | not null default
nextval('dhcp_subnet_id_seq'::text)ipaddress   | cidr    |description | text    |
Indices: dhcp_subnet_id_key,        dhcp_subnet_ipaddress_idx
dhcp=# \d dhcp_subnet_hp   Table "dhcp_subnet_hp" Attribute  | Type | Modifier
-------------+------+----------ipaddress   | text |description | text |
dhcp=# INSERT INTO dhcp_subnet(
dhcp(# ipaddress,
dhcp(# description)
dhcp-# SELECT
dhcp-# ipaddress,
dhcp-# description
dhcp-# FROM dhcp_subnet_hp;
ERROR:  Attribute 'ipaddress' is of type 'cidr' but expression is of type
'text'       You will need to rewrite or cast the expression
dhcp=# INSERT INTO dhcp_subnet(
dhcp(# ipaddress,
dhcp(# description)
dhcp-# SELECT
dhcp-# CAST(ipaddress AS cidr),
dhcp-# description
dhcp-# FROM dhcp_subnet_hp;
ERROR:  Cannot cast type 'text' to 'cidr'



pgsql-sql by date:

Previous
From: Jeff Putsch
Date:
Subject: Help with UPDATE syntax
Next
From: Sondaar Roelof
Date:
Subject: RE: Some questions about PLpgSql