Movatterモバイル変換


[0]ホーム

URL:


You don’t need a vector database - just use Postgres for everything. Read the case study on switching from Pinecone to Neon
PostgreSQL Tutorial
PostgreSQL Tutorial

PostgreSQL LEFT() Function

The PostgreSQLLEFT() function returns the firstn characters in the string.

Syntax

The following illustrates the syntax of the PostgreSQLLEFT() function:

LEFT(string, n)

Arguments

The PostgreSQLLEFT() 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.

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

Return value

The PostgreSQLLEFT() function returns the firstn characters in a string.

Examples

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

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

SELECT LEFT('ABC',1);

The result is

left------ A(1 row)

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

SELECT LEFT('ABC',2);

Here is the result:

left------ AB(1 row)

The following statement demonstrates how to use a negative integer:

SELECT LEFT('ABC',-2);

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

left------ A(1 row)

See the following customer table in the sample database:

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

SELECT LEFT(first_name, 1)initial,    COUNT(*)FROM customerGROUP BY initialORDER BY initial;

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

PostgreSQL LEFT example

Remarks

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

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

Last updated on

Was this page helpful?
Thank you for your feedback!

[8]ページ先頭

©2009-2025 Movatter.jp