Skip to content

LPAD

Pads a expr string on the left with characters or strings from the pad argument to the length len.

Syntax

sql
LPAD(expr, len [, pad])

Arguments

expr: A value of data type STRING.


len: The total length of the result as a numeric argument or an argument of another data type that can be converted to a numeric data type, evaluated as an integer. In some multibyte character sets, the display length of a string may differ from len.


pad (optional): A value of data type STRING. Characters/bytes from this parameter are used to pad the base.

Return Value

The return value is of data type STRING.

Usage Notes

  • If expr is longer than len, expr is truncated to the length len.

  • The pad can be multiple characters/bytes long. The pad argument is repeated in the result until the desired length len is reached. Any excess characters/bytes in the pad argument are truncated.

  • If expr is a string and pad is not specified, ' ' (a single space) is used.

Example 1

sql
SELECT  col1, 
        rpad(col1, 7, ' ') as result1,  
        length(rpad(col1, 7, ' ')) as result1_len,   
        rpad(col1, 13, '@') as result2,
        length(rpad(col1, 13, '@')) as result2_len,
        rpad(col1, 13, 'ab') as result3,
        length(rpad(col1, 13, 'ab')) as result3_len
        FROM (
                select           'Lorem'       as col1
                union all select 'ipsum dolor' as col1
                union all select '7391.91'     as col1);
+-------------+---------+-------------+---------------+-------------+---------------+-------------+
| COL1        | RESULT1 | RESULT1_LEN | RESULT2       | RESULT2_LEN | RESULT3       | RESULT3_LEN |
+-------------+---------+-------------+---------------+-------------+---------------+-------------+
| Lorem       | Lorem   | 7           | Lorem@@@@@@@@ | 13          | Loremabababab | 13          |
| ipsum dolor | ipsum d | 7           | ipsum dolor@@ | 13          | ipsum dolorab | 13          |
| 7391.91     | 7391.91 | 7           | 7391.91@@@@@@ | 13          | 7391.91ababab | 13          |
+-------------+---------+-------------+---------------+-------------+---------------+-------------+