LPAD


(BMC.DB2.SPE2410)

The LPAD function pads an input string to the left with a specified character string or blanks.

Padding occurs only if the actual length of the input string is less than an integer, and the pad is not an empty string.

When the current length of the input string is less than the specified length, the result is padded with a pad string in the same encoded scheme as the input string.

image-2024-7-18_12-22-32.png

The arguments are defined as follows:

stringExpression

Specifies the rightmost characters of an input string

The stringExpression must be in one of the following data types:

  • CHAR
  • VARCHAR
  • GRAPHIC
  • VARGRAPHIC
  • BINARY
  • VARBINARY
  • SMALLINT
  • INTEGER
  • BIGINT

The resultant string has the same data type as the input string, in various formats.

length

An integer constant that specifies the length of the result. The value must be zero or a positive integer that is less than or equal to n, where n is 32704 if the input string is a character or binary string, or where is 16352 if the input string is a graphic string.

pad

An expression that specifies the string with which to pad. The expression must return a built-in character string, graphic string, or binary string.

The data type of the input string determines the default pad string:

  • The SBCS blank character, if the input string is a character string.
  • The ideographic blank character, if the input string is a graphic string. For a graphic string in an EUC database, X'3000' is used. For graphic strings in a Unicode database, X'0020' is used.
  • Hexadecimal zero (X'00'), if the input string is a binary string.
  • The length of the pad string should be greater than zero otherwise it results in error.

Result of the LPAD function

The data type of the resultant string depends on the data type of the stringExpression:

Input string data type 

Resultant string data type

VARCHAR or CHAR

VARCHAR

VARGRAPHIC or GRAPHIC

VARGRAPHIC

VARBINARY or BINARY

VARBINARY

The result of the function is a varying-length string that has the same CCSID of string-expression. String expression and Pad should have the same data type and the same encoding scheme. If the string expression or pad is FOR BIT DATA, no character conversion occurs.

The length attribute of the result depends on the integer. If an integer is greater than 0, the length attribute of the result is an integer. If the integer is 0, the length attribute of the result is 1.

The actual length of the resultant string is determined by the specified integer. If an integer is 0, the actual length is 0, and the result is the empty result string. If the integer is less than the actual length of the input string, the actual length is an integer and the result is truncated.

The resultant string is nullable if any argument is nullable and results in a null value if any argument is null.

 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*