Skip to main content

SPLIT_PART

Splits source string based upon a seperator and returns the given part (counting from one).

See also:
[SELECT]


Syntax

SELECT SPLIT_PART(source, sep, part) FROM <multipartIdentifier>;

Arguments

source
A literal string or string column that you want to perform the split on.

sep
A character or characters you want to use as the separator.

If sep is composed of multiple characters, each character is treated as separator.

Only ASCII (1-byte) symbols are supported as separators.

part
The element (one based) of the split string to return. Negative values are used to count backwards from the last element.

Return Value(s)

Returns a string.


Examples

Extract elements from a string seperated by a forward slash

SELECT name,  
split_part(id, '/', 3) as subscription,
split_part(id, '/', 5) as resource_group,
json_extract(properties, '$.hardwareProfile.vmSize') as vm_size
FROM azure.compute.virtual_machines
WHERE resourceGroupName = 'stackql-ops-cicd-dev-01'
AND subscriptionId = '273769f6-545f-45b2-8ab8-2f14ec5768dc';

Extract the last element from a url

SELECT
name,
SPLIT_PART(network, '/', -1) as network,
SPLIT_PART(region, '/', -1) as region
FROM google.compute.vpn_gateways WHERE project = 'stackql-demo';

For more information, see https://github.com/nalgeon/sqlean/blob/main/docs/text.md