STARTS_WITH

Return true when the input string begins with the given prefix, false otherwise.

Category: stringReturns: BOOLEANDialect: PostgreSql

Syntax

STARTS_WITH(str, prefix)

Description

## Overview Returns true when the input string begins with the given prefix, and false otherwise. STARTS_WITH (underscore form) is the common SQL name; STARTSWITH (no underscore) is an equivalent alias. The match is literal and case-sensitive; for pattern-based prefix matching use LIKE 'prefix%'. Prefix tests are natural fits for partitioning namespaces (URLs, file paths, identifier codes) and for dispatching rows into category buckets based on a known-length leading code. ## Behavior - Returns NULL when either argument is NULL. - Returns true for an empty prefix: the empty string is a prefix of every string. - Returns false when the prefix is longer than the input string. - The match is case-sensitive and literal; SQL wildcards and regex metacharacters are treated as ordinary characters. - Operates on Unicode code points; multi-byte UTF-8 characters are matched correctly. - Short-circuits on the first mismatched character. ## Compatibility - Semantically equivalent to str LIKE prefix || '%' after wildcard escaping. - Semantically equivalent to LEFT(str, LENGTH(prefix)) = prefix for non-NULL inputs.

Parameters

NameTypeDescription
strSpecifies the input string to test.
prefixSpecifies the prefix to check for. The match is case-sensitive and literal.

Examples

-- Matching prefix
SELECT STARTS_WITH('hello world', 'hello');  -- true
-- Non-matching prefix
SELECT STARTS_WITH('hello world', 'world');  -- false
-- Case-sensitive
SELECT STARTS_WITH('Hello', 'hello');  -- false
-- Empty prefix always matches
SELECT STARTS_WITH('hello', '');  -- true
-- NULL propagates
SELECT STARTS_WITH(CAST(NULL AS VARCHAR), 'x');  -- NULL
-- Filter URLs by protocol
SELECT url
FROM ops.etl.staging_links
WHERE STARTS_WITH(url, 'https://');

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →