Home > Database > Mysql Tutorial > body text

How can we use MySQL's LPAD() and RPAD() functions in the same query to pad both the left and right sides of a string to both sides of the original string?

WBOY
Release: 2023-08-23 09:33:03
forward
1028 people have browsed it

How can we use MySQLs LPAD() and RPAD() functions in the same query to pad both the left and right sides of a string to both sides of the original string?

For achieving this, we must have to use one of the functions as the 1st argument of the other function. In other words, either RPAD() function would be the 1st argument of LPAD() function or LPAD() function would be the 1st argument of RPAD() function. It can be understood with the help of the following example

Example

mysql> Select RPAD(LPAD(' My name is Ram ',23,'* '),30,'* ');
+------------------------------------------------+
| RPAD(LPAD(' My name is Ram ',23,'* '),30,'* ') |
+------------------------------------------------+
| * * * * My name is Ram * * * *                 |
+------------------------------------------------+
1 row in set (0.00 sec)
Copy after login

In the query above, LPAD() is the first argument of RPAD() function.

mysql> Select LPAD(RPAD(' My name is Ram ',23,'* '),30,'* ');
+------------------------------------------------+
| LPAD(RPAD(' My name is Ram ',23,'* '),30,'* ') |
+------------------------------------------------+
| * * * * My name is Ram * * * *                 |
+------------------------------------------------+
1 row in set (0.00 sec)
Copy after login

在上述查询中,RPAD() 是 LPAD() 函数的第一个参数。

The above is the detailed content of How can we use MySQL's LPAD() and RPAD() functions in the same query to pad both the left and right sides of a string to both sides of the original string?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!