- Categories:
IPADDRESS functions (Network)
IPADDRESS_ TRUNC¶
Returns the network address of an IPADDRESS value for a given
prefix length. The function keeps the leading bits of the address that the prefix length specifies and
sets the remaining host bits to zero. For example, truncating 192.168.129.255 to a prefix length of
17 returns 192.168.128.0.
Use this function to group addresses by subnet, to mask the host bits of addresses so that they’re less precise, and to match addresses against networks that are stored as a network address and a prefix length.
See also:
Syntax¶
Arguments¶
ip_addressAn expression of type
IPADDRESS.prefix_lengthAn integer expression that specifies the number of leading bits of the address to keep:
- For an IPv4 address, specify a value from 0 through 32.
- For an IPv6 address, specify a value from 0 through 128.
The prefix length doesn’t need to be a constant. For example, you can pass a column that stores a prefix length for each row.
Returns¶
Returns a value of type IPADDRESS with the same address family as the input.
If either argument is NULL, the function returns NULL.
Usage notes¶
-
The valid range of the prefix length depends on the address family of each input value. If the prefix length is outside that range, the function raises an error rather than returning
NULL. For example, a prefix length of 48 is valid for an IPv6 address but raises the following error for an IPv4 address:If Snowflake can detect the invalid prefix length when it compiles the query, the query fails with a SQL compilation error instead. This happens when the prefix length is a constant outside 0 through 128, or when both arguments are constants. For example,
IPADDRESS_TRUNC(IPADDRESS '192.0.2.1', 33)returns the following error: -
If a column contains both IPv4 and IPv6 addresses, choose the prefix length for each address family separately. Put the choice in the
prefix_lengthargument, for example with IFF, so that each row receives a prefix length that’s valid for its own family. To test whether a value is an IPv4 address, compare it with the IPv4 range:For an example, see Handle tables that contain both IPv4 and IPv6 addresses.
-
A prefix length of 32 for an IPv4 address, or 128 for an IPv6 address, returns the input unchanged. A prefix length of 0 returns
0.0.0.0for an IPv4 address and::for an IPv6 address. -
The function returns a single
IPADDRESSvalue, the network address, not a CIDR range. The result doesn’t record the prefix length. For other limitations, see Limitations for the IPADDRESS data type.
Examples¶
The examples in this section use the following table:
This section contains the following examples:
- Return the network address of an IPv4 address and an IPv6 address
- Group addresses by subnet
- Mask the host bits of addresses
- Join to a table of networks
- Handle tables that contain both IPv4 and IPv6 addresses
Return the network address of an IPv4 address and an IPv6 address¶
The following example truncates an IPv4 address to a prefix length of 17 and an IPv6 address to a prefix length of 49:
Group addresses by subnet¶
The following example counts events for each IPv4 /24 subnet and each IPv6 /48 subnet:
Mask the host bits of addresses¶
The following example sets the host bits of each address to zero, keeping the IPv4 /24 network and the
IPv6 /48 network:
Join to a table of networks¶
The following example matches each event to a table that stores each network as a network address and a prefix length. The network address must have its host bits cleared, or no address matches it.
-
Create the table of networks:
-
Join each event to the network that contains it. Because the table of networks contains both IPv4 and IPv6 networks, the join compares IPv4 events with IPv6 networks. To keep the prefix length valid for an IPv4 event, the query caps it at 32 with LEAST. An IPv4 address never equals an IPv6 network address, so the capped comparison doesn’t produce false matches:
Handle tables that contain both IPv4 and IPv6 addresses¶
A single prefix length is often valid for only one address family. The following query fails because the table contains IPv4 addresses and 48 is outside the 0 through 32 range for IPv4:
To process every row, choose a valid prefix length for each row in the prefix_length argument, as shown
in the earlier examples:
This query returns the same values as the MASKED_IP column in Mask the host bits of addresses.