Filling function for mysqlLPAD and RPAD with insufficient digits

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.