- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
In MySQL, how can we pad a string with another string?
MySQL has two functions namely LPAD() and RPAD() with the help of which we can pad a string with another string.
LPAD() function, as the name suggests, left pads a string with another string. Following is the syntax for using it in MySQL
Syntax
LPAD(original_string, @length, pad_string)
Here,
- original_string is the string in which we pad another string.
- @length is the total length of the string returned after padding.
- Pad_string is the string which is to be padded with original_string.
Example
mysql> Select LPAD('My name is Ram',22,'* '); +--------------------------------+ | LPAD('My name is Ram',22,'* ') | +--------------------------------+ | * * * * My name is Ram | +--------------------------------+ 1 row in set (0.00 sec)
RPAD() function, as the name suggests, right pads a string with another string. Following is the syntax for using it in MySQL.
Syntax
RPAD(original_string, @length, pad_string)
Here,
- original_string is the string in which we pad another string.
- @length is the total length of the string returned after padding.
- Pad_string is the string which is to be padded with original_string.
Example
mysql> Select RPAD('My name is Ram',22,'* '); +--------------------------------+ | RPAD('My name is Ram',22,'* ') | +--------------------------------+ | My name is Ram* * * * | +--------------------------------+ 1 row in set (0.00 sec)
Advertisements