DB2 Scalar functions - LPAD
The LPAD function pads a string on the left with a specified character string or with blanks.
The schema is SYSIBM.
The LPAD function treats leading or trailing blanks in the string expression as significant. Padding will only occur if the actual length of string-expression is less than integer, and pad is not an empty string.
The data type of the string expression determines the default pad string:
The data type of the result depends on the data type of the string expression:
The result of the function is a varying length string that has the same string unit and code page as the string expression. The values for the string expression and the pad expression must have compatible data types. If the string expression and pad expression have different code pages, then the pad expression is converted to the code page of the string expression. If either the string expression or the pad expression is FOR BIT DATA or a binary string, no character conversion occurs.
The length attribute of the result depends on whether the value for integer is available when the SQL statement containing the function invocation is compiled (for example, if it is specified as a constant or a constant expression) or available only when the function is executed (for example, if it is specified as the result of invoking a function). When the value is available when the SQL statement containing the function invocation is compiled, if integer is greater than zero, the length attribute of the result is integer. If integer is 0, the length attribute of the result is 1. When the value is available only when the function is executed, the length attribute of the result is determined according to the following table:
The actual length of the result is determined from integer. If integer is 0 the actual length is 0, and the result is the empty result string. If integer is less than the actual length of string-expression, the actual length is integer and the result is truncated.
If any argument can be null, the result can be null; if any argument is null, the result is the null value.
Chris
Meg
Jeff
SELECT LPAD(NAME,15,'.' ) AS NAME FROM T1;
returns:
NAME --------------- ..........Chris ............Meg ...........Jeff
SELECT LPAD(NAME,5,'.' ) AS NAME FROM T1;
NAME ----- Chris ..Meg .Jeff
NAME ----- Chris Meg Jeff
SELECT LPAD(NAME,15,'123' ) AS NAME FROM T1;
NAME --------------- 1231231231Chris 123123123123Meg 12312312312Jeff
SELECT LPAD(NAME,4,'.' ) AS NAME FROM T1;
NAME ---- Chri .Meg Jeff