In order to use the LPAD() or RPAD() function with column values, we need to specify the column name as the first argument to these functions. It will be clearer by following the example in "Student" table -
mysql> Select Name, LPAD(Name,10,'*') from student; +---------+-------------------+ | Name | LPAD(Name,10,'*') | +---------+-------------------+ | Gaurav | ****Gaurav | | Aarav | *****Aarav | | Harshit | ***Harshit | | Gaurav | ****Gaurav | | Yashraj | ***Yashraj | +---------+-------------------+ 5 rows in set (0.08 sec) mysql> Select Name, RPAD(Name,10,'*') from student; +---------+-------------------+ | Name | RPAD(Name,10,'*') | +---------+-------------------+ | Gaurav | Gaurav**** | | Aarav | Aarav***** | | Harshit | Harshit*** | | Gaurav | Gaurav**** | | Yashraj | Yashraj*** | +---------+-------------------+ 5 rows in set (0.00 sec)
We can also use these two functions in one query to get the value of the column as shown below-
mysql> Select Name, RPAD(LPAD(Name,10,'* '),14,'* ') from student; +---------+----------------------------------+ | Name | RPAD(LPAD(Name,10,'* '),14,'* ') | +---------+----------------------------------+ | Gaurav | * * Gaurav* * | | Aarav | * * *Aarav* * | | Harshit | * *Harshit* * | | Gaurav | * * Gaurav* * | | Yashraj | * *Yashraj* * | +---------+----------------------------------+ 5 rows in set (0.00 sec)
The above is the detailed content of How can we use LPAD() or RPAD() function with values from a column of a MySQL table?. For more information, please follow other related articles on the PHP Chinese website!