// strings reference

SQL SPLIT_PART function

Returns one field of a delimiter-separated string by position. Only PostgreSQL has it built in; the other engines get there with table-valued splitters, SUBSTRING_INDEX or regexes.

카테고리
Strings
Returns
string
In this category
11 of 172

구문

SPLIT_PART(string, delimiter, field_number)  -- PostgreSQL

엔진 지원

엔진Support메모
SQL Server Different name or syntax STRING_SPLIT (2016+) returns rows; its ordinal column (2022+) picks the nth
PostgreSQL 예 Supported under this name.
MySQL Different name or syntax SUBSTRING_INDEX with nesting: SUBSTRING_INDEX(SUBSTRING_INDEX(s, ',', 2), ',', -1)
SQLite Not built in No built-in equivalent.
Oracle Different name or syntax REGEXP_SUBSTR(s, '[^,]+', 1, n) picks the nth match

예시

PostgreSQL

SELECT SPLIT_PART('a-b-c', '-', 2) AS part;  -- b

MySQL

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('a-b-c', '-', 2), '-', -1) AS part;  -- b

SQL Server

-- SQL Server 2022+: the third argument adds the ordinal column
SELECT value FROM STRING_SPLIT('a-b-c', '-', 1)
WHERE ordinal = 2;  -- b

알아 두면 좋은 정보

  • SPLIT_PART is 1-based: field 1 is the first piece. An out-of-range field returns an empty string rather than an error, and from PostgreSQL 14 a negative field counts from the end: SPLIT_PART('a-b-c', '-', -1) is 'c'.
  • MySQL's nested SUBSTRING_INDEX works in two steps: the inner call keeps the first n pieces, the outer call takes the last piece of that.
  • SQL Server's STRING_SPLIT makes no promise about row order unless you pass the third enable_ordinal argument (SQL Server 2022+), which adds an ordinal column. Before 2022, numbering the rows with ROW_NUMBER is not reliable.
  • Oracle's REGEXP_SUBSTR(s, '[^,]+', 1, n) skips empty fields: for 'a,,c' field 2 comes back as 'c', not ''.

Questions about SPLIT_PART

Does SQL Server have SPLIT_PART?

No. STRING_SPLIT (2016+) breaks the string into rows. On SQL Server 2022+, pass 1 as the third argument and filter on the ordinal column. On older versions, JSON_VALUE on a string rebuilt as a JSON array, or CHARINDEX arithmetic, gives a reliable position.

How do I split a string into rows?

PostgreSQL: unnest(string_to_array(s, ',')). SQL Server: STRING_SPLIT. MySQL: a numbers table with SUBSTRING_INDEX. SQLite: json_each on a JSON-encoded array, or a recursive CTE.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.