LENGTH()

SQL · String Functions

Theory

LENGTH(string) returns how many characters a string has. LENGTH('Asha') is 4. Every character counts, including spaces at the start or end, so LENGTH(' Ravi ') is 6. That makes LENGTH a quick way to detect hidden padding: compare LENGTH(name) with LENGTH(TRIM(name)) and any difference means stray spaces.

The name differs by dialect. SQL Server uses LEN(), which also ignores trailing spaces, and has DATALENGTH() for bytes. In MySQL, LENGTH() counts bytes, not characters, so text in Hindi or with emojis gives a bigger number than expected; use CHAR_LENGTH() there. Postgres, Oracle, SQLite and Snowflake LENGTH() count characters. LENGTH(NULL) returns NULL, not 0, and LENGTH('') is 0 (except in Oracle, where '' is NULL). Common uses are validation (a 10-digit mobile number must have LENGTH = 10) and filtering out empty values.

Why we use it

Lengths reveal data problems: hidden spaces, truncated codes, phone numbers with missing digits, empty strings that look like data. LENGTH lets you check and filter on these in plain SQL.

When to use it

Use LENGTH for validation rules, finding padded or empty values and with SUBSTRING arithmetic. In MySQL use CHAR_LENGTH for character counts; in SQL Server remember LEN() ignores trailing spaces.

How it works

  1. Take the input string
  2. Is it NULL? Yes: Return NULL. No: Start counting.
  3. Count each character, spaces included
  4. Counts bytes (MySQL)? Yes: Hindi/emoji count more than 1. No: Each character counts 1.
  5. Return the length

Syntax

LENGTH(string)

Example

SELECT name, LENGTH(name) 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