2024年4月17日发(作者:)

postgre 拆分路径

## PostgreSQL Split Paths.

PostgreSQL provides several functions to help you split

paths into their component parts. The most common function

used for this purpose is `string_to_array()`. This function

takes a string as input and returns an array of substrings,

delimited by a specified delimiter.

For example, the following query uses

`string_to_array()` to split a path into an array of

directory names:

SELECT string_to_array('/usr/local/bin', '/') AS

directory_names;

This query would return the following result:

{["usr", "local", "bin"]}。

The `string_to_array()` function can also be used to

split a path into an array of file extensions. For example,

the following query uses `string_to_array()` to split a

file path into an array of file extensions:

SELECT string_to_array('/usr/local/bin/bash', '.') AS

file_extensions;

This query would return the following result:

{["bash"]}。

In addition to `string_to_array()`, PostgreSQL also

provides several other functions that can be used to split

paths. These functions include:

`split_part()`: This function returns a specified part

of a path. For example, the following query uses

`split_part()` to return the directory name of a file path:

SELECT split_part('/usr/local/bin/bash', '/', 2);