CHAR_LENGTH()

SQL · String Functions

Theory

CHAR_LENGTH(string), also written CHARACTER_LENGTH, is the SQL-standard function that returns the number of characters in a string. It always counts characters, never bytes. The difference matters for text outside basic English. In UTF-8, an English letter takes 1 byte but a Hindi letter takes 3, so 'जयपुर' is 5 characters but 15 bytes.

In MySQL this is where people get caught: LENGTH('जयपुर') returns 15 (bytes) while CHAR_LENGTH returns 5. Postgres and MySQL support CHAR_LENGTH; Postgres LENGTH is already character-based. SQLite has no CHAR_LENGTH, but its LENGTH counts characters for text and bytes for a BLOB, which the diagram uses to show both. The standard function for byte count is OCTET_LENGTH. Note that a character here is a Unicode code point, so a Hindi letter with a vowel sign (like पु) counts as 2.

Why we use it

Column size limits, form validation and SUBSTRING positions are about characters, not bytes. CHAR_LENGTH gives the true character count regardless of language, which avoids bugs when your data includes Hindi names or emojis.

When to use it

Use CHAR_LENGTH (or a character-based LENGTH) for validation and display width. Use OCTET_LENGTH or a byte function only when you care about storage size. In SQL Server use LEN; in SQLite use LENGTH on text.

How it works

  1. Take the input string
  2. Is it NULL? Yes: Return NULL. No: Decode it as characters.
  3. Count characters (code points), not bytes
  4. Multi-byte letters such as Hindi still count as 1 each
  5. Return the character count

Syntax

CHAR_LENGTH(string)

Example

SELECT CHAR_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