MySQL LPAD() Function

In MySQL, the LPAD() function left pads a string with a string to the specified length. If you want to right pad a string, use the RPAD() function.

LPAD() Syntax

Here is the syntax of MySQL LPAD() function:

LPAD(str, len, padstr)

Parameters

str
Required. the string to pad out.
len
Required. The length to pad to.
padstr
Optional. The string to pad with.

Return value

The LPAD() function left pads a string with a string to the specified length and returns the left padded string.

  • If len less than the length of str, str will be truncated to the length len.
  • If len is negative, the function will return NULL.
  • The function will return NULL if either parameter is NULL.

LPAD() Examples

Here are some examples of MySQL LPAD() function.

SELECT
    LPAD('oh', 10, 'o'),
    LPAD('oh', 1, 'o'),
    LPAD('oh', -1, 'o'),
    LPAD('World', 15, 'Hello')\G
       LPAD('oh', 10, 'o'): oooooooooh
        LPAD('oh', 1, 'o'): o
       LPAD('oh', -1, 'o'): NULL
LPAD('World', 15, 'Hello'): HelloHelloWorld