Home / SQL / Reference / Text functions

SQL INSTR

Return the position of some text inside another, or 0.

Syntax

INSTR(text, search)

Combine with SUBSTR to cut text at a character.

Example

Query (on the shop database used in the SQL course)

SELECT name, INSTR(name, ' ') AS space_at, SUBSTR(name, 1, INSTR(name, ' ') - 1) AS first_name
FROM customers
LIMIT 4;

Result

namespace_atfirst_name
Aarav Sharma6Aarav
Mia Chen4Mia
Liam Walker5Liam
Sita Gurung5Sita

4 rows

Good to know. PostgreSQL: POSITION(search IN text) or STRPOS(text, search).

Related

Learn it step by step in the free lesson Functions: Text, Numbers, Dates & CASE, with exercises that check your queries.