How can I use another MySQL function/s with REPEAT() function?


Suppose if we want to make the output of REPEAT() function more readable then we can use another function/s with it. For example, if we want to add space or some other character between the repeated values then we can use CONCAT() function.

Example

mysql> Select REPEAT(CONCAT(' *',Subject,'* '),3)AS Subject_repetition from student;
+-----------------------------------------+
| Subject_repetition                      |
+-----------------------------------------+
| *Computers* *Computers* *Computers*     |
| *History* *History* *History*           |
| *Commerce* *Commerce* *Commerce*        |
| *Computers* *Computers* *Computers*     |
| *Math* *Math* *Math*                    |
+-----------------------------------------+
5 rows in set (0.00 sec)

In the example below, we are using QUOTE() and CONCAT() function both with REPEAT() function:

mysql> Select REPEAT(QUOTE(CONCAT(' *',Subject,'* ')),3)AS Subject_repetition from student;
+-----------------------------------------------+
| Subject_repetition                            |
+-----------------------------------------------+
| ' *Computers* '' *Computers* '' *Computers* ' |
| ' *History* '' *History* '' *History* '       |
| ' *Commerce* '' *Commerce* '' *Commerce* '    |
| ' *Computers* '' *Computers* '' *Computers* ' |
| ' *Math* '' *Math* '' *Math* '                |
+-----------------------------------------------+
5 rows in set (0.00 sec)

In this way, by using other function/s with REPEAT() function, we can make the output more readable.

Updated on: 07-Feb-2020

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements