Home > Database > Mysql Tutorial > 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?

王林
Release: 2023-09-08 17:25:06
forward
1561 people have browsed it

我们如何将 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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template