TRIM
The TRIM function trims leading or trailing characters (or both) from a character string.
SQL syntax
There are four syntax options for TRIM:
-
You can specify one of the
TRIMqualifiers (LEADINGorTRAILINGorBOTH) with theTrim_character:TRIM ( LEADING|TRAILING|BOTH
Trim_characterFROMExpression) -
You can specify one of the
TRIMqualifiers (LEADINGorTRAILINGorBOTH) without theTrim_character. In this case,Trim_characterdefaults to a blank.TRIM ( LEADING|TRAILING|BOTH FROMExpression) -
You can specify the
Trim_characterwithout one of theTRIMqualifiers, which removes both leading and trailing instances ofTrim_characterfromExpression.TRIM (
Trim_characterFROMExpression) -
You can specify the
Expressionwithout a qualifier or aTrim_character, which results in leading and trailing blank spaces removed fromExpression.TRIM (Expression)
Parameters
TRIM has the parameters:
| Parameter | Description |
|---|---|
|
|
|
|
|
If specified, |
|
|
|
Description
-
If
Expressionis of typeCHARorVARCHAR2, the data type returned isVARCHAR2. IfExpressionis of typeNCHARorNVARCHAR2, the data type returned isNVARCHAR2. IfExpressionis of typeCLOB, the data type returned isCLOB. IfExpressionis of typeNCLOB, the data type returned isNCLOB. The returned data type length is equal to the data type length ofExpression. -
If
Expressionis a data type defined withCHARlength semantics, the returned length is expressed inCHARlength semantics. -
If either
Trim_characterorExpressionisNULL, the result isNULL. -
You can specify
TT_CHAR,TT_VARCHAR,TT_NCHAR, andTT_NVARCHARforTrim_characterandExpression. IfExpressionis of typeTT_CHARorTT_VARCHAR, the data type returned isTT_VARCHAR. IfExpressionis of typeTT_NCHARorTT_NVARCHAR, the data type returned isTT_NVARCHAR. -
If
Trim_characteris of typeNCHARorNVARCHAR2andExpressionis of typeCHARorVARCHAR2, thenTrim_characteris demoted toCHARorVARCHAR2beforeTRIMis invoked. The conversion ofTrim_charactercould be lost. IfTrim_characteris not in the database character set, then the query may produce unexpected results. -
For
CHAR,VARCHAR2,NCHAR,NVARCHAR2,CLOBandNCLOBtypes:-
If all the characters in
Expressionare removed by theTRIMfunction, the result isNULL.
-
-
For
TT_CHAR,TT_VARCHAR,TT_NCHARandTT_NVARCHARtypes:-
If all the characters in
Expressionare removed by theTRIMfunction, the result is the empty string.
-
Examples
Use TRIM function with qualifier to remove Trim_character '0' from Expression '0000TRIM Example0000':
Command> SELECT TRIM (LEADING '0' FROM '0000TRIM Example0000') FROM dual; < TRIM Example0000 > 1 row found. Command> SELECT TRIM (TRAILING '0' FROM '0000TRIM Example0000') FROM dual; < 0000TRIM Example > 1 row found. Command> SELECT TRIM (BOTH '0' FROM '0000TRIM Example0000') FROM dual; < TRIM Example > 1 row found.
Use TRIM function with qualifier to remove blank spaces. Do not specify a Trim_character. Default value for Trim_character is blank space:
Command> SELECT TRIM (LEADING FROM ' TRIM Example ') FROM dual; < TRIM Example > 1 row found. Command> SELECT TRIM (TRAILING FROM ' TRIM Example ') FROM dual; < TRIM Example > 1 row found. Command> SELECT TRIM (BOTH FROM ' TRIM Example ') FROM dual; < TRIM Example > 1 row found.
Use TRIM function with Trim_character '0'. Do not specify a qualifier. Leading and trailing '0's are removed from Expression '0000TRIM Example0000':
Command> SELECT TRIM ('0' FROM '0000TRIM Example0000') FROM dual;
< TRIM Example >
1 row found.
Use TRIM function without a qualifier or Trim_character. Leading and trailing spaces are removed.
< TRIM Example >
1 row found.
Command> SELECT TRIM (' TRIM Example ') FROM dual;