SelectName,LPAD(Name,10,'*')fromstudent;+---------+---------- ---------+|Name |LPAD(Name,10,'*')|+---------+--------------- ---"/> SelectName,LPAD(Name,10,'*')fromstudent;+---------+---------- ---------+|Name |LPAD(Name,10,'*')|+---------+--------------- ---">

Home  >  Article  >  Database  >  How can we use LPAD() or RPAD() function with values ​​from a column of a MySQL table?

How can we use LPAD() or RPAD() function with values ​​from a column of a MySQL table?

王林
王林forward
2023-09-08 17:25:061532browse

我们如何将 LPAD() 或 RPAD() 函数与 MySQL 表的列中的值一起使用?

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 -

Example

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete