我们如何在同一查询中使用MySQL的LPAD()和RPAD()函数,将字符串的左侧和右侧都填充到原始字符串的两侧?

数据库数据库 2023-08-31 03:48:26 564
摘要: Forachievingthis,wemusthavetouseoneofthefunctionsasthe1stargumentoftheotherfunction.Inotherwords,eitherRPAD()functionwouldbethe1stargumentofLPAD()functionorLPAD()fu...

我们如何在同一查询中使用MySQL的LPAD()和RPAD()函数,将字符串的左侧和右侧都填充到原始字符串的两侧?

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)

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)

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

以上就是我们如何在同一查询中使用MySQL的LPAD()和RPAD()函数,将字符串的左侧和右侧都填充到原始字符串的两侧?的详细内容,更多请关注其它相关文章!