Home  >  Article  >  Database  >  How to use LPAD() and RPAD() functions in MySQL

How to use LPAD() and RPAD() functions in MySQL

WBOY
WBOYforward
2023-05-26 18:13:251686browse

LPAD()/RPAD()

LPAD(str,len,padstr)The function indicates that the left side of the string str is filled with padstr until the length is len;RPAD(str,len,padstr)The function means padstr is used on the right side of the string str until the length is len. For example:

SELECT LPAD(123, 6, '0') AS str1, LPAD(123, 2, '0') AS str2,
       RPAD(123, 6, '0') AS str1, RPAD(123, 2, '0') AS str1;
str1  |str2|str1  |str1|
------+----+------+----+
000123|12  |123000|12  |

When the length of the string str is greater than len, it is equivalent to truncating the string from the right side.

In addition, the REPEAT(str,count) function is used to copy the string str count times and return the result. For example:

SELECT REPEAT('?', 5) AS str;
str       |
----------+
?????|

The above is the detailed content of How to use LPAD() and RPAD() functions in MySQL. For more information, please follow other related articles on the PHP Chinese website!

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