repeat() function in SQL

The repeat() function in SQL is used to repeat a string a specified number of times. This function can be helpful for generating test data, formatting output, or creating visual effects in textual data presentations.

Syntax

The syntax for the repeat() function is as follows:

This function takes two arguments: the string to repeat and the number of times to repeat it.

Here’s a basic example:

SELECT repeat('Hello', 3)

repeat() for visual separator

Imagine you need to generate a visual separator in a report generated from SQL queries. You can use the repeat() function to create a line of dashes:

SELECT repeat('-', 20) AS separator

This query will produce a string of 20 dashes, which can be used as a divider between sections of a report.

repeat() for credit card placeholder

The same way way we can generate a credit card placeholder instead of showing a real credit card number in our research:

SELECT repeat('X', 16) AS credit_card_number

The repeat() function isn’t something you’d use every day for Data Analysis, but it’s a versatile tool in SQL for enhancing the manipulation and presentation of string data, offering extensive possibilities for creative and practical applications.

Database compatibility

MySQL
PostgreSQL
SQLite
Redshift
Big Query
Snowflake
repeat
repeat
MySQL
PostgreSQL
SQLite
Redshift
Big Query
Snowflake

Find a problem on this page?

Want to get more involved? SQL MDN Docs is an open-source project, check out contribution guidelines.
This page was last modified on April 12, 2024.