Skip to content

IPv4 ​

IPv4 stores a validated IPv4 address. Address ordering compares numeric address values rather than their text spellings, so 10.0.0.2 sorts before 10.0.0.10.

Syntax ​

text
IPv4

Invalid addresses raise TYPE_MISMATCH. Cast an IPv4 value to UInt32 to obtain its numeric address, or to BLOB to obtain its four network-order bytes. Direct arithmetic and numeric aggregation require an explicit conversion.

An IPv4 address compares equal to its IPv4-mapped IPv6 equivalent. Use inet and cidr functions for addresses with subnet prefixes.

Examples ​

Inspecting an address ​

sql
SELECT CAST('10.0.0.2' AS IPv4),
       CAST(CAST('10.0.0.2' AS IPv4) AS UInt32),
       hex(CAST('10.0.0.2' AS IPv4))
FORMAT csv;
text
"10.0.0.2",167772162,"0A000002"

Sorting addresses ​

sql
CREATE TABLE clients(ip IPv4);
INSERT INTO clients VALUES ('10.0.0.10'), ('10.0.0.2');
sql
SELECT ip
FROM clients
ORDER BY ip
FORMAT csv;
text
"10.0.0.2"
"10.0.0.10"

See IPv6 and IP address functions.