StringsPostgreSQL only

STARTS_WITH

Checks whether a string starts with the given prefix.

Syntax

STARTS_WITH(<text>, <prefix>)
<text> ^@ <prefix>

Returns true or false. It does the same as LIKE '<prefix>%', but you don't have to think about % and _ in the prefix itself: it's compared literally. The ^@ operator is a shorter way to write the same thing.

Data for the examples

employees

namedepartmentsalarybonusphone
Emmabackend1200300+44 7700 900123
Liambackend1000NULLNULL
Oliviafrontend1350150+44 7700 900456
Noahfrontend950NULL+44 7700 900789
Sophiadesign1200200NULL

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

Does it start with the prefix
SELECT STARTS_WITH('PostgreSQL', 'Post') AS post,
STARTS_WITH('PostgreSQL', 'post') AS lowercase;
postlowercase
truefalse
Phone numbers with the +44 code
SELECT name, phone
FROM employees
WHERE STARTS_WITH(phone, '+44');
namephone
Emma+44 7700 900123
Olivia+44 7700 900456
Noah+44 7700 900789
When the prefix contains _
SELECT STARTS_WITH('userX1', 'user_') AS starts_with,
'userX1' LIKE 'user_%' AS like_matches;
starts_withlike_matches
falsetrue

In LIKE, _ means “any character”, so userX1 matches. STARTS_WITH compares the prefix literally.

Watch out

Case matters: STARTS_WITH('Emma', 'em') gives false. To ignore case — STARTS_WITH(LOWER(name), 'em').

For NULL the result is NULL, so in WHERE such a row simply won't pass.

Related functions