MySQL: LPAD Function

  • Post author:
  • Post category:MySQL
  • Post comments:1 Comment
MySQL LPAD Function

In this guide, we will explain how to use the MySQL LPAD function with syntax and examples.

Description

The MySQL LPAD function returns a string that is left-padded with a specified string to a certain length.

Syntax

The syntax for the LPAD function in MySQL is:

LPAD( string, length, pad_string )

Parameters or Arguments

string

The string to left-pad.

length

The length of the result after string has been left-padded.

pad_string

The specified string to left-pad to string.

Note

  • If string is longer than length, the LPAD function will remove characters from string to shorten it to length characters.
  • See also the RPAD function.

Applies To

The LPAD function can be used in the following versions of :

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

Example

Let’s look at some LPAD function examples and explore how to use the LPAD function.

For example:

mysql> SELECT LPAD('adglob.in', 11, 'A');
Result: 'AAadglob.in'

mysql> SELECT LPAD('adglob.in', 12, 'A');
Result: 'AAAadglob.in'

mysql> SELECT LPAD('abc', 6, ' ');
Result: '   abc'

mysql> SELECT LPAD('abc', 9, 'XYZ');
Result: 'XYZXYZabc'

mysql> SELECT LPAD('abc', 10, 'XYZ');
Result: 'XYZXYZXabc'

Next Topic : Click Here

This Post Has One Comment

Leave a Reply