RPAD()

SQL · String Functions

Theory

RPAD(string, length, pad) adds the pad character(s) to the right of a string until it reaches the target length. RPAD('IT', 5, '-') returns 'IT---'. Like LPAD, a string that is already longer than length is cut down to length, so RPAD('Marketing', 5, '.') returns 'Marke'. The pad can be more than one character and is repeated as needed.

RPAD is available in MySQL, Postgres, Oracle and Snowflake, but not in SQL Server or SQLite. In those you append enough padding and then cut to the length you want: SUBSTR(dept || '........', 1, 8) in SQLite, or LEFT(dept + REPLICATE('.', 8), 8) in SQL Server. Here the emulation matches RPAD exactly, because both keep the left part. Typical uses are fixed-width text files (each field padded to an exact column width), aligned plain-text reports, and dot leaders such as 'Sales......'.

Why we use it

Some systems still exchange fixed-width files where every field must be exactly n characters, and plain-text reports need aligned columns. RPAD makes each value exactly the right width so columns line up.

When to use it

Use RPAD for fixed-width exports, aligned text output and dot leaders. Set the length to fit your longest value, or it will be cut. For screen display in BI tools, alignment is better done in the tool than in SQL.

How it works

  1. Take the string, target length and pad character
  2. Is the string longer than the target? Yes: Cut it to the target length. No: Continue.
  3. Count how many characters are missing
  4. Repeat the pad on the right to fill the gap
  5. Return the fixed-width string

Syntax

RPAD(string, length, pad_char)

Example

SELECT RPAD('IT', 5, '-');
-- 'IT---'

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