
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- How can we stuff a string with another one using MySQL functions?
- Left pad a String with a specified String in Java
- In MySQL, how can we check whether a string of a specified pattern is not present within another string?
- How can we replace all the occurrences of a substring with another substring within a string in MySQL?
- In MySQL, how can we use FROM_UNIXTIME() function with format string?
- Left pad a String in Java with zeros
- How can we check if specific string occurs multiple times in another string in Java?
- How can we extract a substring from a string in MySQL?
- Can we replace a number with a String in a MySQL result set?
- MySQL number-string formatting to pad zeros on the left of a string with numbers after a slash
- Right pad a string in Java
- Left pad a string in Java
- Left pad a String with a specified character in Java
- Left pad a String with spaces (' ') in Java
- How can we retrieve the length of a specified string in MySQL?

Advertisements