 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we use MySQL LPAD() and RPAD() functions in the same query for padding the string to both sides, left and right, 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) 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) In the query above, RPAD() is the first argument of LPAD() function.
Advertisements
 