PostgreSQL: How to Select First N Characters in a String


Often in PostgreSQL you may want to extract the first N characters in a string field.

You can use the following syntax to do so:

SELECT LEFT(team, 3) AS first_three
FROM athletes;

This particular example will extract the first three characters from each string in the team column of the table named athletes.

The resulting substring will be saved to a new column named first_three.

To select a different number of characters from the beginning of a string, simply change the 3 in the LEFT function to a different number.

The following example shows how to use this syntax in practice.

Example: How to Select First N Characters in a String in PostgreSQL

Suppose that we create the following table named athletes that contains information about various basketball teams:

-- create table named 'athletes'
CREATE TABLE athletes (
  athleteID INTEGER PRIMARY KEY,
  team TEXT,
  points INTEGER,
  assists INTEGER
);

-- add rows to the table 
INSERT INTO athletes VALUES (0001, 'Mavs', 22, 5);
INSERT INTO athletes VALUES (0002, 'Hawks', 30, 12);
INSERT INTO athletes VALUES (0003, 'Kings', 12, 3);
INSERT INTO athletes VALUES (0004, 'Kings', 25, 9);
INSERT INTO athletes VALUES (0005, 'Mavs', 31, 14);
INSERT INTO athletes VALUES (0006, 'Mavs', 16, 3);
INSERT INTO athletes VALUES (0007, 'Celtics', 10, 5);
INSERT INTO athletes VALUES (0008, 'Celtics', 14, 13);
INSERT INTO athletes VALUES (0009, 'Nuggets', 20, 8);
INSERT INTO athletes VALUES (0010, 'Hawks', 26, 3);

-- view all rows in table
SELECT * FROM athletes;

 athleteid |  team   | points | assists 
-----------+---------+--------+---------
         1 | Mavs    |     22 |       5
         2 | Hawks   |     30 |      12
         3 | Kings   |     12 |       3
         4 | Kings   |     25 |       9
         5 | Mavs    |     31 |      14
         6 | Mavs    |     16 |       3
         7 | Celtics |     10 |       5
         8 | Celtics |     14 |      13
         9 | Nuggets |     20 |       8
        10 | Hawks   |     26 |       3

Suppose that we would like to extract the first three characters from each string in the team column.

We can use the following syntax to do so:

SELECT team,
       LEFT(team, 3) AS first_three
FROM athletes;

  team   | first_three 
---------+-------------
 Mavs    | Mav
 Hawks   | Haw
 Kings   | Kin
 Kings   | Kin
 Mavs    | Mav
 Mavs    | Mav
 Celtics | Cel
 Celtics | Cel
 Nuggets | Nug
 Hawks   | Haw

Notice that the resulting table contains a new column named first_three that contains the first three characters from each string in the team column.

Feel free to change the 3 in the LEFT function to instead select a different number of characters from the beginning of the string.

Additional Resources

The following tutorials explain how to perform other common tasks in PostgreSQL:

PostgreSQL: Extract Substring Before Specific Character
PostgreSQL: Extract Substring After Specific Character
PostgreSQL: How to Extract Substring Between Two Characters

Leave a Reply