STRPOS()
All functions > STRING > STRPOS()
Returns the position of a substring within a string, optionally finding the N-th occurrence.
Signatures
Returns: Position of the substring (1-indexed), or 0 if not found
STRPOS(string: VARCHAR, substring: VARCHAR, [occurrence: BIGINT]) → BIGINT sql
| Parameter | Type | Required | Description |
|---|---|---|---|
string | VARCHAR | Yes | String to search in |
substring | VARCHAR | Yes | Substring to find |
occurrence | BIGINT | No | Which occurrence to find (1 for first, 2 for second, etc.) - optional |
Notes
- Position is 1-indexed
- Returns 0 if substring not found
- Case-sensitive search
- Optional
occurrencefinds the N-th match; same semantics on every SQL backend - If any argument is NULL the result is NULL (use
NULL(VARCHAR)/NULL(BIGINT); bareNULLfails inference) - See also: POSITION
Examples
FeatureQL
SELECT
-- Basic search
f1 := STRPOS('Hello World', 'World'),
-- First occurrence
f2 := STRPOS('Hello Hello World', 'Hello'),
-- Explicit first occurrence
f3 := STRPOS('Hello Hello World', 'Hello', 1),
-- Second occurrence
f4 := STRPOS('Hello Hello World', 'Hello', 2),
-- Third occurrence
f5 := STRPOS('ABCABCABC', 'ABC', 3),
-- Not found
f6 := STRPOS('test', 'xyz'),
-- NULL yields NULL
f7 := STRPOS(NULL(VARCHAR), 'a')
;Result
| f1 BIGINT | f2 BIGINT | f3 BIGINT | f4 BIGINT | f5 BIGINT | f6 BIGINT | f7 BIGINT |
|---|---|---|---|---|---|---|
| 7 | 1 | 1 | 7 | 7 | 0 | NULL |
On this page