Skip to main content

split_comma_separated

Schema: lib

Description

Split a comma-separated string into an array of strings. It also removes any leading or trailing whitespace from each item in the resulting array. This function is useful for using a comma-separated string with Postgres's built-in array operations.

Usage

Signature

lib.split_comma_separated(val text)

Parameters

NameTypeDescription
valtextThe comma-separated string to be split into an array.

Results

This function returns an array of strings, with each item representing a value from the original comma-separated string. The resulting array will have any leading or trailing whitespace removed from each item.

Examples

Example 1: Splitting a comma-separated string
SELECT lib.split_comma_separated('a,b,c') AS split_vals;

Output:

split_vals
{a,b,c}
Example 2: Whitespace handling while splitting a comma-separated string
SELECT lib.split_comma_separated('a, b, c ') AS split_vals;

Output:

split_vals
{a,b,c}
Example 3: Getting all contract logs for any contract matching the comma-separated result
select *
from contract_logs cl
where cl.consensus_time > now() - '1 hour'::interval
and cl.contract = any(lib.split_comma_separated('0xaf88d065e77c8cc2239327c5edb3a432268e5831,0xc7abb2c5f3bf3ceb389df0eecd6120d451170b50'));