Movatterモバイル変換


[0]ホーム

URL:


You don’t need a vector database - just use Postgres for everything. Read the case study on switching from Pinecone to Neon
PostgreSQL Tutorial
PostgreSQL Tutorial

PostgreSQL REPEAT() Function

Summary: in this tutorial, you will learn how to use the PostgreSQLREPEAT() function to repeat a string a specified number of times.

Introduction to PostgreSQL REPEAT() function

In PostgreSQL, theREPEAT() function repeats a string a specified number of times.

Here’s the basic syntax of theREPEAT() function:

REPEAT(string,number)

In this syntax:

  • string. The string that you want to repeat.
  • number. The number of times that you want to repeat thestring in the resulting string.

TheREPEAT() function returns a string that repeatsnumber times. If thenumber is less than 1, the function returns an empty string.

Ifstring ornumber isNULL, theREPEAT() function returnsNULL.

TheREPEAT() function can be particularly useful when you want to format the data for display.

PostgreSQL REPEAT() function examples

Let’s explore some examples of using theREPEAT() function.

1) Basic REPEAT() function example

The following example uses theREPEAT() function to repeat the letter “A” there times:

SELECT REPEAT('A',3);

Output:

repeat-------- AAA(1 row)

In this example, theREPEAT() function returns a string"AAA" that repeats the letter"A" three times.

2) Using the REPEAT() function to draw a bar chart

We’ll use thefilm table from thesample database for the demonstration:

PostgreSQL REPEAT() Function - Sample TableThe following example uses theREPEAT() function to create a bar chart illustrating the counts of films based on their ratings:

SELECT  rating,  count(film_id),  REPEAT(    '*',    (      COUNT(film_id)/ 10    ) ::INT  ) chartFROM  filmGROUP BY  rating;

Output:

rating | count |         chart--------+-------+------------------------ PG-13  |   223 | ********************** NC-17  |   210 | ********************* R      |   195 | ******************* G      |   178 | ***************** PG     |   194 | *******************(5 rows)

Summary

  • Use theREPEAT() function to repeat a string a specified number of times.

Last updated on

Was this page helpful?
Thank you for your feedback!

[8]ページ先頭

©2009-2025 Movatter.jp