TRANSLATE()

SQL · String Functions

Theory

TRANSLATE(string, from_chars, to_chars) replaces single characters using a position-by-position mapping: the 1st character of from_chars becomes the 1st character of to_chars, the 2nd becomes the 2nd, and so on. TRANSLATE('abc123', '123', 'xyz') returns 'abcxyz'. All mappings happen in one pass, so TRANSLATE(date_text, '/.', '--') turns both '12/04/2025' and '12.04.2025' into '12-04-2025'.

This is different from REPLACE, which swaps whole substrings. In Postgres and Oracle, characters in from_chars with no partner in to_chars are deleted, so TRANSLATE(phone, '- ', '') would remove dashes and spaces (Oracle needs a trick because an empty string is NULL there). SQL Server's TRANSLATE requires both lists to be the same length. MySQL and SQLite have no TRANSLATE, so you chain REPLACE calls, one per character, as the diagram does.

Why we use it

Cleaning often means swapping many single characters at once: different date separators, accented letters to plain ones, masking every digit. TRANSLATE does all of them in one call instead of a long chain of REPLACE calls.

When to use it

Use TRANSLATE for one-to-one character swaps or deleting a set of characters. Use REPLACE when the text to change is longer than one character (like '.com'), and chain REPLACE where TRANSLATE is not available.

How it works

  1. Take the string, from_chars and to_chars
  2. Read the next character of the string
  3. Is it in from_chars? Yes: Swap for matching to_char (or drop). No: Keep it unchanged.
  4. Repeat to the end of the string
  5. Return the translated string

Syntax

TRANSLATE(string, from_chars, to_chars)

Example

SELECT TRANSLATE('abc123', '123', 'xyz');
-- 'abcxyz'

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