SUBSTRING()

SQL · String Functions

Theory

SUBSTRING(string, start, length) returns a piece of a string: start is the position of the first character you want (positions start at 1, not 0) and length is how many characters to take. SUBSTRING('2025-04-12', 1, 4) returns '2025' and SUBSTRING('2025-04-12', 6, 2) returns '04'. If you leave out length, you get everything from start to the end of the string.

Names vary: SQLite and Oracle use SUBSTR, SQL Server uses SUBSTRING and requires the length argument, and MySQL and Postgres accept both. The SQL-standard form is SUBSTRING(string FROM start FOR length). SQLite and Oracle also accept a negative start that counts from the end, so SUBSTR(x, -4) gives the last 4 characters. Combined with INSTR or POSITION, SUBSTRING can cut at a character whose position varies, such as everything before the '@' in an email.

Why we use it

Real data often packs several facts into one string: year and month in a date stored as text, a state code inside an ID, a domain inside an email. SUBSTRING pulls out exactly the part you need.

When to use it

Use SUBSTRING when the piece you need is at a known or calculable position. For fixed prefixes or suffixes LEFT/RIGHT read more clearly where available. For real dates, prefer date functions (EXTRACT, YEAR) over cutting text.

How it works

  1. Take string, start position and optional length
  2. Start past the end? Yes: Return an empty string. No: Move to the start position.
  3. Was a length given? Yes: Take that many characters. No: Take everything to the end.
  4. Return the extracted piece

Syntax

SUBSTRING(string, start, length)

Example

SELECT SUBSTRING(name, 1, 4) 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