PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • Aggregate Functions
    • Date / Time Functions
    • String Functions
    • Math Functions
Home / PostgreSQL String Functions / PostgreSQL SPLIT_PART

PostgreSQL SPLIT_PART

The PostgreSQL SPLIT_PART() function splits a string on a specified delimiter and returns the nth substring.

Syntax

The following illustrates the syntax of the PostgreSQL SPLIT_PART() function:

1
SPLIT_PART(string, delimiter, position)

Arguments

The SPLIT_PART() function requires three arguments:

1) string

is the string to be split.

2) delimiter

The delimiter is a string used as the delimiter for splitting.

3) position

is the position of the part to return, starting from 1. The position must be a positive integer.

If the position is greater than the number of parts after splitting, the SPLIT_PART() function returns an empty string.

Return Value

The SPLIT_PART() function returns a part as a string at a specified position.

Examples

See the following statement:

1
SELECT SPLIT_PART('A,B,C', ',', 2);

The string 'A,B,C' is split on the comma delimiter (,) that results in 3 substrings: ‘A’, ‘B’, and ‘C’.

Because the position is 2, the function returns the 2nd substring which is ‘B’.

Here is the output:

PostgreSQL SPLIT_PART example

See the following payment table in the sample database.

payment table

The following statement uses the SPLIT_PART() function to return the year and month of the payment date:

1
2
3
4
5
6
SELECT
    split_part(payment_date::TEXT,'-', 1) y,
    split_part(payment_date::TEXT,'-', 2) m,
    amount
FROM
    payment;

The output is:

PostgreSQL SPLIT_PART function example

In this tutorial, you have learned how to use the PostgreSQL SPLIT_PART() function to get part of a string at a specified position after splitting.

Previous Tutorial: PostgreSQL Substring
Next Tutorial: PostgreSQL REPLACE

PostgreSQL String Functions

  • ASCII
  • CONCAT
  • CHR
  • FORMAT
  • LEFT
  • LENGTH
  • LPAD
  • MD5
  • POSITION
  • REGEXP_MATCHES
  • REGEXP_REPLACE
  • RIGHT
  • REPLACE
  • SPLIT_PART
  • SUBSTRING
  • TRANSLATE
  • TRIM

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL TRUNC
  • PostgreSQL ROUND
  • PostgreSQL Math Functions
  • PostgreSQL LOCALTIME
  • PostgreSQL LOCALTIMESTAMP
  • PostgreSQL CURRENT_TIMESTAMP
  • PostgreSQL CURRENT_TIME
  • PostgreSQL CURRENT_DATE
  • PostgreSQL Date Functions
  • PostgreSQL EXTRACT

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.