LTRIM

The LTRIM function removes Start of changeany of the specified charactersEnd of change from the beginning of an expression.

Read syntax diagramSkip visual syntax diagramLTRIM(string-expression ,trim-expression )

Start of changeThe LTRIM function removes all of the characters that are contained in trim-expression from the beginning of string-expression. The collating sequence does not affect the search. If the string-expression is defined as FOR BIT DATA or is a binary data type, the search is done by comparing each byte in trim-expression to the byte at the beginning of string-expression.End of change

string-expression
An expression that returns a value of any built-in numeric, Start of changedatetime,End of change or string data type. A numeric or datetime argument is cast to a character string before evaluating the function.1 For more information about converting numeric Start of changeor datetimeEnd of change to a character string, see VARCHAR.
Start of changetrim-expressionEnd of change
Start of changeAn expression that specifies the characters to remove from the beginning of string-expression. The expression must return a value of any built-in numeric, datetime, or string data type. A numeric or datetime argument is cast to a character string before evaluating the function.
When trim-expression is not specified, the data type of the string-expression determines the default value used:
  • Hexadecimal zero (X'00') if the argument is a binary string.
  • DBCS blank if the argument is a DBCS graphic string.
  • UTF-16 or UCS-2 blank if the first argument is a Unicode graphic string.
  • UTF-8 blank if the first argument is a UTF-8 character string.
  • Otherwise, a SBCS blank.
End of change

Start of changeThe value for string-expression and the value for trim-expression must have compatible data types. For more information about data type compatibility, see Assignments and comparisons. If string-expression and trim-expression have different CCSIDs, trim-expression is converted to the CCSID of string-expression.End of change

The data type of the result depends on the data type of string-expression:

Data type of string-expression Data type of the Result
CHAR or VARCHAR VARCHAR
CLOB CLOB
GRAPHIC or VARGRAPHIC VARGRAPHIC
DBCLOB DBCLOB
BINARY or VARBINARY VARBINARY
BLOB BLOB

The length attribute of the result is the same as the length attribute of string-expression. Start of changeThe actual length of the result for character or binary strings is the length of string-expression minus the number of bytes removed. The actual length of the result for graphic strings is the length of string-expression minus the number of graphic characters removed.End of change If all characters are removed, the result is an empty string.

Start of changeIf either argument can be null, the result can be null; if any argument is null, the result is the null value.End of change

The CCSID of the result is the same as that of string-expression.

Examples

  • Example 1: Use the LTRIM function when the host variable HELLO is defined as CHAR(9) and has a value of
     '    Hello'
      VALUES LTRIM(:HELLO)
    The result is 'Hello'. When a trim-expression is not specified only blanks are removed.
  • Example 2: Use the LTRIM function to remove individual numbers in the trim-expression from the beginning (left side) of the string-expression.
    SELECT LTRIM ('123DEFG123', '321'),
    			 LTRIM ('12DEFG123', '321'),
    			 LTRIM ('123123222XYZ22', '123'),
    			 LTRIM ('12321', '213'),
    			 LTRIM ('XYX123 ', '321')
       FROM SYSIBM.SYSDUMMY1
    The result is:
     'DEFG123'
     'DEFG123'
     'XYZ22'
     '' (an empty string - all characters removed)
     'XYX123' (no characters removed)
    The LTRIM function does not remove instances of '1', '2', and '3' on the right side of the string, following characters that are not '1', '2', or '3'.
  • Example 3: Use the LTRIM function to remove the characters specified in the trim-expression from the beginning of the string-expression.
      VALUES LTRIM(('...$V..$AR', '$.'))
    The result is 'V..$AR'. The function stops when it encounters a character not in the trim-expression.
  • Example 4: Use the LTRIM function to remove the characters specified in the trim-expression from the beginning of the string-expression.
      VALUES LTRIM('[[ -78]]', '- []')
    The result is '78]]'. When removing characters and blanks, you must include a blank in the trim-expression.
1 The LTRIM function with one argument returns the same results as: STRIP(string-expression,LEADING)