POSITION()

SQL · String Functions

Theory

POSITION(substring IN string) returns the position where substring first appears inside string, counting from 1. POSITION('@' IN 'asha@techtown.in') returns 5 because '@' is the fifth character. If the substring is not found, the result is 0, and if either argument is NULL, the result is NULL. The search is case-sensitive in most databases.

POSITION is the SQL-standard spelling, supported by Postgres, MySQL and Snowflake. Other names do the same job: INSTR(string, substring) in SQLite, MySQL and Oracle (note the arguments are the other way round), CHARINDEX(substring, string) in SQL Server, and STRPOS(string, substring) in Postgres. On its own, POSITION is often used as a check: a result of 0 means the character is missing, which flags emails without '@'. More often it is used with SUBSTRING to cut a string at the found position.

Why we use it

The part you need often sits before or after a separator whose position changes from row to row, such as '@' in emails or '-' in codes. POSITION tells you exactly where the separator is so you can cut there, or flag rows where it is missing.

When to use it

Use POSITION to find a separator before slicing with SUBSTRING, or to check that a value contains something. For simple 'contains' filters, LIKE '%@%' is easier to read. Remember the argument order changes with INSTR and CHARINDEX.

How it works

  1. Take the substring to find and the string
  2. Scan the string from position 1
  3. Does the substring start at this position? Yes: Return the position. No: Move one character right.
  4. Reached the end without a match? Yes: Return 0. No: Keep scanning.
  5. Position returned (0 if not found)

Syntax

POSITION(substring IN string)

Example

SELECT POSITION('@' IN 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