REPEAT Function in SQL

The REPEAT string function shows the string in the output to the given number of times.

Syntax of REPEAT String Function

Syntax1: This syntax uses the REPEAT function with the column name of the SQL table:

In the syntax, we have to specify the name of that column on which we want to perform the REPEAT function.

Syntax2: This syntax uses the REPEAT function with the string:

Syntax2: This syntax uses the REPEAT function with the individual character:

Examples of REPEAT String function

Example 1: The following SELECT query shows the JavaTpoint string four times in the result:

Output:

REPEAT_4_string
JAVATPOINT JAVATPOINT JAVATPOINT JAVATPOINT

Example 2: The following SELECT query shows the given set of words two times in the output:

Output:

REPEAT_2_string
NEW DELHI IS THE CAPITAL OF INDIANEW DELHI IS THE CAPITAL OF INDIA

Example 3: The following SELECT query shows the S character five times in the result:

Output:

REPEAT_5_character
SSSSS

Example 4: The following SELECT query shows the set of numbers six times in the result:

Output:

REPEAT_6_numbers
9825 9825 9825 9825 9825 9825

Example 5: This example uses the REPEAT function with the table in Structured Query Language.

To understand the REPEAT function with SQL, we have to create the SQL table first using CREATE statement. The syntax for creating the new table in the SQL database is as follows:

The following CREATE statement creates the Student_Marks table:

The below INSERT queries insert the records of college Faculties in the Student_Marks table:

The following SELECT statement displays the inserted records of the above Student_Marks table:


Schoolboy_IdSchoolboy_First_NameSchoolboy_Middle_NameSchoolboy_Last_NameSchoolboy_ClassSchoolboy_CitySchoolboy_StateSchoolboy_Marks
4001AmanRoySharma4ChandigarhPunjab88
4002VishalGurrSharma8MurthalHaryana95
4007RajsinghaniaGupta6GhaziabadUttar Pradesh91
4004YashChopraSinghania9JaipurRajasthan85
4011VinaySharmaRoy8ChandigarhPunjab94
4006ManojSinghaniaGupta5GhaziabadUttar Pradesh83
4010RamRaheemGupta9LucknowUttar Pradesh89

Query 1: The following SELECT query uses the REPEAT function with the Student_First_Name column of the above Student_Marks table:

This SQL statement shows the First name of each student three times in the result.

Output:

Student_First_NameREPEAT_3FirstName
AmanAmanAmanAman
VishalVishalVishalVishal
RajRajRajRaj
YashYashYashYash
VinayVinayVinayVinay
ManojManojManojManoj
RamRamRamRam

Query 2: The following SELECT query uses the REPEAT function with the Student_Last_Name column of the above Student_Marks table:

This SQL statement shows the last name of each student two times in the result.

Output:

Student_Last_NameREPEAT_2LastName
SharmaSharmaSharma
SharmaSharmaSharma
GuptaGuptaGupta
SinghaniaSinghaniaSinghania
RoyRoyRoy
GuptaGuptaGupta
GuptaGuptaGupta

Query 3: The following SELECT query uses the REPEAT function with the Student_Address column of the above Student_Marks table:

This SQL statement shows the state four times of each student in the result.

Output:

Student_StateREPEAT_4State
PunjabPunjabPunjabPunjabPunjab
HaryanaHaryanaHaryanaHaryanaHaryana
Uttar PradeshUttar PradeshUttar PradeshUttar PradeshUttar Pradesh
RajasthanRajasthanRajasthanRajasthanRajasthan
PunjabPunjabPunjabPunjabPunjab
Uttar PradeshUttar PradeshUttar PradeshUttar PradeshUttar Pradesh
Uttar PradeshUttar PradeshUttar PradeshUttar PradeshUttar Pradesh

Query 4: The following SELECT query uses the REPEAT function with the Student_Middle_Name and Student_City column of the above Student_Marks table:

This SQL statement shows the middle name two times and the city three times for each student in the result.

Output:

Student_middle_NameREPEAT_2MiddlenameStudent_CityREPEAT_3City
RoyRoyRoyChandigarhChandigarhChandigarhChandigarh
GurrGurrGurrMurthalMurthalMurthalMurthal
singhaniasinghaniasinghaniaGhaziabadGhaziabadGhaziabadGhaziabad
ChopraChopraChopraJaipurJaipurJaipurJaipur
SharmaSharmaSharmaChandigarhChandigarhChandigarhChandigarh
SinghaniaSinghaniaSingahniaGhaziabadGhaziabadGhaziabadGhaziabad
RaheemRaheemRaheemLucknowLucknowLucknowLucknow





Latest Courses