All Products
Search
Document Center

MaxCompute:LPAD

Last Updated:Mar 26, 2026

Left-pads a string to a specified length. This function is available in MaxCompute V2.0.

Syntax

string lpad(string <str1>, int <length>, string <str2>)

Parameters

ParameterRequiredTypeDescription
str1YesSTRINGThe string to left-pad.
lengthYesINTThe total number of characters in the output string.
str2YesSTRINGThe padding string.

Return value

Returns a STRING. If length is greater than the length of str1 and less than the total number of characters in str1 and str2, the function left-pads str1 with str2 to reach the specified length. If length is less than the length of str1, the result is truncated to length characters from the left. If length is 0, an empty string is returned. If any input parameter is null, null is returned.

Examples

Example 1: Left-pad abcdefgh with the string 12 to 10 characters.

-- Returns 12abcdefgh
SELECT lpad('abcdefgh', 10, '12');

Example 2: Truncate when length is less than the length of str1.

-- Returns abcde (truncated from the left to 5 characters)
SELECT lpad('abcdefgh', 5, '12');

Example 3: Partial padding when length is between the length of str1 and the combined length of str1 and str2.

-- Returns 1abcdefgh (pads with only part of str2)
SELECT lpad('abcdefgh', 9, '12');

Example 4: length set to 0.

-- Returns an empty string
SELECT lpad('abcdefgh', 0, '12');

Example 5: Null input.

-- Returns null
SELECT lpad(null, 0, '12');

Related functions

LPAD is a string function. For more information about functions related to string searches and conversion, see String functions.