LPAD function
Left-pads a string to a specified length with a specified padding string.
Syntax
LPAD(string, length [, pad_string])
Arguments
string: The string to pad.length: The desired total length of the result string.pad_string: Optional. The string to use for padding. Default is a single space' '.
Returns
A VARCHAR of the specified length, with the original string right-aligned and padded on the left.
NULL Handling
If any argument is
NULL, returnsNULL.
Limits
lengthmust be non-negative. Negative values will cause an error.Binary/VARBINARYtypes are not supported.
Examples
-- Example 1: Pad with spaces (default)
> SELECT LPAD('abc', 10);
' abc'
-- Example 2: Pad with asterisks
> SELECT LPAD('abc', 10, '*');
'*******abc'
-- Example 3: Pad with multi-character string
> SELECT LPAD('abc', 10, '-*=');
'-*=-*=-abc'
-- Example 4: UTF-8 support
> SELECT LPAD('¼¼¼a', 10, '智者');
'智者智者智者¼¼¼a'
-- Example 5: Using with expressions
> SELECT LPAD(LTRIM(' goog goog'), LENGTH(' goog goog'));
' goog goog'
-- Example 6: NULL handling
> SELECT LPAD(NULL, 10, 'ab');
NULL
> SELECT LPAD('ab', 10, NULL);
NULL
> SELECT LPAD('hello', NULL, 'ab');
NULL
-- Example 7: Negative length causes error
> SELECT LPAD('abc', -15);
ERROR
See Also
LTRIM function - Remove leading characters
TRIM function - Remove leading and trailing characters