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
| name | space_at | first_name |
|---|---|---|
| Aarav Sharma | 6 | Aarav |
| Mia Chen | 4 | Mia |
| Liam Walker | 5 | Liam |
| Sita Gurung | 5 | Sita |
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.