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 LEFT Function

PostgreSQL LEFT Function

The PostgreSQL LEFT() function returns the first n characters in the string.

Syntax

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

1
LEFT(string, n)

Arguments

The PostgreSQL LEFT() function requires two arguments:

1) string

is a string from which a number of the leftmost characters returned.

2) n

is an integer that specifies the number of left-most characters in the string should be returned.

If n is negative, the LEFT() function returns the leftmost characters in the string but last |n| (absolute) characters.

Return value

The PostgreSQL LEFT() function returns the first n characters in a string.

Examples

Let’s look at some examples of using the LEFT() function.

The following example shows how to get the first character of a string 'ABC':

1
SELECT LEFT('ABC',1);

The result is

1
2
3
4
left
------
A
(1 row)

To get the first two characters of the string ‘ABC’, you use 2 instead of 1 for the n argument:

1
SELECT LEFT('ABC',2);

Here is the result:

1
2
3
4
left
------
AB
(1 row)

The following statement demonstrates how to use a negative integer:

1
SELECT LEFT('ABC',-2);

In this example, n is -2, therefore, the LEFT() function return all character except the last 2 characters, which results in:

1
2
3
4
left
------
A
(1 row)

See the following customer table in the sample database:

The following statement uses the LEFT() function to get the initials and the COUNT() function to return the number of customers for each initial.

1
2
3
4
5
SELECT LEFT(first_name, 1) initial,
    COUNT(*)
FROM customer
GROUP BY initial
ORDER BY initial;

In this example, first, the LEFT() function returns initials of all customers. Then, the GROUP BY clause groups customers by their initials. Finally, the COUNT() function returns the number of customer for each group.

PostgreSQL LEFT example

Remarks

If you want to get the n rightmost characters, please see the RIGHT() function for the details.

In this tutorial, you have learned how to use the PostgreSQL LEFT() function to get the n left-most characters in a string.

Previous Tutorial: PostgreSQL MD5 Function
Next Tutorial: PostgreSQL RIGHT Function

PostgreSQL String Functions

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

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 ANY Operator
  • PostgreSQL EXISTS
  • How To Delete Duplicate Rows in PostgreSQL
  • PostgreSQL TO_CHAR Function
  • PostgreSQL TO_NUMBER Function
  • PostgreSQL TO_TIMESTAMP Function
  • PostgreSQL CEIL Function
  • PostgreSQL MOD Function
  • PostgreSQL FLOOR Function
  • PostgreSQL ABS Function

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.