In mysql, The LPAD (s1, len, s2) function fills string s2 at the beginning of s1, so that the string length reaches len, and then returns string s1. Unlike LPAD, RPAD is filled at the end of the string. This article introduces how to use the padding function of mysqlLPAD and RPAD with insufficient digits. For more information, see. MySQL string filling function LPAD (s1, len, s2)
The LPAD (s1, len, s2) function fills string s2 at the beginning of s1, so that the length of the string reaches len, and then returns string s1. If the length of string s1 is greater than len, the return value is shortened to len length.
Instance:
Use the LPAD function to fill the string. The SQL statement is as follows:
mysql>SELECT LPAD('www.baike369.com',12,'??'),LPAD('baike369.com',16,'+-');
The execution result is as follows:
The code execution result shows that:
- The length of the string "www.baikeyun.com" is greater than 12 and does not need to be filled. Therefore, LPAD ('www .baikeyun.com ', 12 ,'?? ') Returns only the shortened string "www. baike369 ".
- The length of the string "shortkedomain.com" is less than 16. Therefore, LPAD ('shortke369. com ', 16,' +-'), and the returned result is "+-+ -baikeyun.com". fill in "+-" on the left side of the string and the length is 16.
MySQL string filling function RPAD (s1, len, s2)
The RPAD (s1, len, s2) function fills string s2 at the end of s1, so that the length of the string reaches len, and then returns string s1. If the length of string s1 is greater than len, the return value is shortened to len length.
Instance:
Use the RPAD function to fill the string. The SQL statement is as follows:
mysql>SELECT RPAD('www.baike369.com',12,'+-'),RPAD('baike369.com',16,'??');
The execution result is as follows:
The code execution result shows that:
- The length of the string "www.baikebei.com" is greater than 12 and does not need to be filled. Therefore, RPAD ('www .baikebei.com ', 12,' + -') returns only the four-character substring "www. baike369 ".
- The length of the string "shortkedomain.com" is less than 16. Therefore, RPAD ('shortke369. com', 16 ,'?? ') The returned result is "your kebab.com ????", Fill "?" on the right of the string, The length is 16.