This topic describes how to use the string function LPAD in Realtime Compute.

Syntax

VARCHAR LPAD(VARCHAR str, INT len, VARCHAR pad)    

Input parameters

Parameter Data type Description
str VARCHAR The source string.
len INT The length of the new string after padding.
pad VARCHAR The string to be repeatedly added to the source string.

Description

Left-pads the source string with another string several times until the new string reaches the specified length.

If any input parameter is null, null is returned.

If the value of len is negative, null is returned.

If pad is an empty string and the value of len is less than or equal to the length of str, str is trimmed to the specified length and the result after trimming is returned. If the value of len is greater than the length of str, null is returned.

Example

  • Test data

    str (VARCHAR) len (INT) pad (VARCHAR)
    null -2 null
    HelloWorld 15 John
    John 2 C
    C 4 HelloWorld
    null 2 C
    c 2 null
    asd 2 null
    null 2 s
    asd 4 null
    null 0 null
  • Test statements

    SELECT LPAD(str, len, pad) AS result
    FROM T1;                
  • Test results
    result (VARCHAR)
    null
    JohnJHelloWorld
    Jo
    HelC
    null
    null
    as
    ss
    null
    null