IPADDRESS_TRUNC
functionsupportedCore SQL functionsReturns 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: - IPADDRESS data type - TO_IPADDRESS - TRY_TO_IPADDRESS
Syntax
IPADDRESS_TRUNC( <ip_address> , <prefix_length> )
Parameters
| Name | Required | Description |
|---|---|---|
ip_address | required | An expression of type IPADDRESS. |
prefix_length | required | An 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: Invalid prefix length 48 for IPADDRESS_TRUNC; expected 0 through 32 for IPv4 or 0 through 128 for IPv6. 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: SQL compilation error: error line 1 at position 7 invalid argument for function [IPADDRESS_TRUNC] unexpected argument [33] at position 2,
- If a column contains both IPv4 and IPv6 addresses, choose the prefix length for each address family separately. Put the choice in the prefix_length argument, 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: Copy code ip_col BETWEEN IPADDRESS '0.0.0.0' AND IPADDRESS '255.255.255.255' 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.0 for an IPv4 address and :: for an IPv6 address.
- The function returns a single IPADDRESS value, 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.