[go: up one dir, main page]
More Web Proxy on the site http://driver.im/

REPLICATE Function in SQL

The REPLICATE is a string function in SQL. It shows the inputted string in the output to the given number of times.

Syntax of REPLICATE String Function

Syntax1: This syntax describes how to use the REPLICATE with the fields of the structured table.

If we want to perform Replicate function, then we have to specify the name of that column from the table whose values we want to repeat.

Syntax2: This syntax descibes how to use the REPLICATE function with the string or sentence:

Syntax3: This syntax descibes how to use the REPLICATE function with the individual character:

Examples of REPLICATE String function

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

Output:

REPLICATE_4_string
JAVATPOINT JAVATPOINT JAVATPOINT JAVATPOINT

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

Output:

REPLICATE_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:


REPLICATE_5_character
SSSSS

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


REPLICATE_6_numbers
9825 9825 9825 9825 9825 9825

Example 5: In this example, we are going to perform the REPLICATE function on the string and integer columns of the table.

So, first we will create the new table through which we will perform the REPLICATE function.

The below CREATE query shows how to create the new table in the Structured Query Language:

Now, we create the new table Student_Sem_Marks using the above CREATE syntax which helps to store the marks of each semester of college students.

The following multiple INSERT queries inserts the records of students with names and their semester marks:

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


Student_IDStudent_First_NameStudent_Last_NameFirst_SemSecond_SemThird_SemFourth_SemTotalPercentageGrade
11254AkashSharma58.85765.54798.24560.254282.90370B1
11250RamSharan28.24758.98778.54877.254243.03660C2
11255ManojGupta55.84725.54868.24488.785238.42478B1
11257ParulChaudhary85.54798.27862.52535.858281.93565B2
11251MonuSharma72.25463.35436.25896.639268.50567B2
11258SrishtiChaudhary85.58978.47898.78589.698352.5588A2
11260BhaveshBardiya78.25687.65882.96391.365340.24285A2

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

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

Output:

Student_First_NameREPLICATE_3FirstName
AkashAkashAkashAkash
RamRamRamRam
ManojManojManojManoj
ParulParulParulParul
MonuMonuMonuMonu
SrishtiSrishtiSrishtiSrishti
BhaveshBhaveshBhaveshBhavesh

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

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

Output:

Student_Last_NameREPLICATE_2LastName
SharmaSharmaSharma
SharanSharanSharan
GuptaGuptaGupta
ChaudharyChaudharyChaudhary
SharmaSharmaSharma
ChaudharyChaudharyChaudhary
BardiyaBardiyaBardiya

Query 3: The following SELECT query uses the REPLICATE function with the Grade column of the above Student_Sem_Marks table:

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

Output:

GradeREPLICATE_4Grade
B1B1B1B1B1
C2C2C2C2C2
B1B1B1B1B1
B2B2B2B2B2
B2B2B2B2B2
A2A2A2A2A2
A2A2A2A2A2

Query 4: The following SELECT query uses the REPLICATE function with the concatenation of first name and last name column of the above Student_Sem_Marks table:

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

Output:

Student_First_NameStudent_Last_NameREPLICATE_2Studentname
AkashSharmaAkashSharmaAkashSharma
RamSharanRamSharanRamSharan
ManojGuptaManojGuptaManojGupta
ParulChaudharyParulChaudharyParulChaudhary
MonuSharmaMonuSharmaMonuSharma
SrishtiChaudharySrishtiChaudharySrishtiChaudhary
BhaveshBardiyaBhaveshBardiyaBhaveshBardiya