Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

POSITION function - Amazon Redshift
This page has not been translated into your language. Request translation

POSITION function

Returns the location of the specified substring within a string.

See CHARINDEX function and STRPOS function for similar functions.

Syntax

POSITION(substring IN string )

Arguments

substring

The substring to search for within the string.

string

The string or column to be searched.

Return type

The POSITION function returns an INTEGER corresponding to the position of the substring (one-based, not zero-based). The position is based on the number of characters, not bytes, so that multi-byte characters are counted as single characters. POSITION returns 0 if the substring is not found within the string.

Examples

To return the position of the string fish within the word dog, use the following example.

SELECT POSITION('fish' IN 'dog'); +-----------+ | position | +-----------+ | 0 | +-----------+

To return the position of the string fish within the word dogfish, use the following example.

SELECT POSITION('fish' IN 'dogfish'); +-----------+ | position | +-----------+ | 4 | +-----------+

The following example uses the SALES table from the TICKIT sample database. For more information, see Sample database.

To return the number of distinct sales transactions with a commission over 999.00 from the SALES table, use the following example. This command counts commissions greater than 999.00 by checking if the decimal is more than 4 places from the beginning of the commission value.

SELECT DISTINCT POSITION('.' IN commission), COUNT (POSITION('.' IN commission)) FROM sales WHERE POSITION('.' IN commission) > 4 GROUP BY POSITION('.' IN commission) ORDER BY 1,2; +-----------+-------+ | position | count | +-----------+-------+ | 5 | 629 | +-----------+-------+
PrivacySite termsCookie preferences
© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved.