REPLICATE Function in SQLThe 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 FunctionSyntax1: 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 functionExample 1: The following SELECT query shows the JavaTpoint string four times in the result: Output:
Example 2: The following SELECT query shows the given set of words two times in the output: Output:
Example 3: The following SELECT query shows the S character five times in the result:
Example 4: The following SELECT query shows the set of numbers six times in the result:
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:
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:
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:
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:
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:
Next TopicTRUNCATE Function in SQL |
We request you to subscribe our newsletter for upcoming updates.