INSTR()

SQL · String Functions

Theory

INSTR(string, substring) returns the 1-based position of the first occurrence of substring inside string, or 0 if it does not appear. INSTR('ravi@gmail.com', '@') is 5. It does the same job as POSITION but uses function-call style with the string first, which is easy to mix up when you switch between the two.

INSTR exists in SQLite, MySQL and Oracle. Oracle's version takes extra optional arguments for a start position and an occurrence number (for example the second '-'). SQL Server uses CHARINDEX(substring, string) and Postgres uses STRPOS(string, substring) or POSITION. The most common pattern is splitting a string at a separator: SUBSTR(email, 1, INSTR(email, '@') - 1) gives the username, and SUBSTR(email, INSTR(email, '@') + 1) gives the domain. Guard against 0 when the separator might be missing, or the slices give odd results.

Why we use it

Splitting strings at a separator is a daily cleaning task: user and domain from emails, area and number from phone codes, first and last name from a full name. INSTR finds the cut point for SUBSTR.

When to use it

Use INSTR with SUBSTR to split strings at a separator in SQLite, MySQL or Oracle. Use POSITION, STRPOS or CHARINDEX in other dialects. Some databases also have SPLIT_PART, which splits by a delimiter directly and is easier to read.

How it works

  1. Take the string and the substring
  2. Find the first place the substring appears
  3. Was it found? Yes: Return its 1-based position. No: Return 0.
  4. Use the position in SUBSTR: before = pos - 1, after = pos + 1
  5. Pieces of the string extracted

Syntax

INSTR(string, substring)

Example

SELECT INSTR(email, '@') FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic