The DB2 TRIM function removes bytes from the beginning, from the end, or from both the beginning and end of a string expression. This DB2 TRIM function can be used with any other character also. It is a scalar function in DB2 that is most often used with strings.
TRIM([LEADING (L) | TRAILING (T) | BOTH (B)] Trim_Character FROM String) RTRIM(String, Trim_Character) LTRIM(String, Trim_Character)
Parameter Name | Description |
LEADING | OPTIONAL. Removes the “trim_character” from the front or left side of the string. |
TRAILING | OPTIONAL. Removes the “trim_character” from the end or right side of the string. |
BOTH | OPTIONAL. Removes the “trim_character” from both(left and right) sides of the string. |
Trim_Character | OPTIONAL. The character that will be removed from the given input string. If this argument is omitted, it will remove whitespace characters from a string. |
String | Required. The input string to trim by the TRIM() function. |
The source string can have any expression from which we extract the final output and which should return whose datatype is built-in one from the list –
In case the specified value of the source string does not belong to any of the above-mentioned datatypes then it is cast to VARCHAR implicitly by the function before the trimmed value of the source string is evaluated.
It is necessary that both the datatypes of the source string and the character to be removed are compatible with each other while using the function. The data type of the output string from the function depends upon the data type of the source string expression and the dependency followed over there is as specified below –
String – ‘ New Year comes with great opportunities ‘. This string has the leading as well as trailing spaces in it. The following statement is used to remove all the blank spaces in the beginning –
SELECT TRIM(LEADING FROM ' New Year comes with great opportunities ') OutputString FROM sysibm.sysdummy1; OR SELECT LTRIM(' New Year comes with great opportunities ') OutputString FROM sysibm.sysdummy1;
The output of the above example is as shown in the below image with no spaces in the beginning –
OutputString: New Year comes with great opportunities
String – ‘ New Year comes with great opportunities ‘. This string has the leading as well as trailing spaces in it. The following statement is used to remove all the blank spaces at the end –
SELECT TRIM(TRAILING FROM ' New Year comes with great opportunities ') OutputString FROM sysibm.sysdummy1; OR SELECT RTRIM(' New Year comes with great opportunities ') OutputString FROM sysibm.sysdummy1;
OutputString: New Year comes with great opportunities
String – ‘ New Year comes with great opportunities ‘. This string has the leading as well as trailing spaces in it. The following statement is used to remove all the blank spaces from both the end –
SELECT TRIM(BOTH FROM ' New Year comes with great opportunities ') OutputString FROM sysibm.sysdummy1;
OutputString: New Year comes with great opportunities
INPUT | OUTPUT |
TRIM(‘ Tech Agilist Blog’) | Tech Agilist Blog |
TRIM(‘Tech Agilist Blog ‘) | Tech Agilist Blog |
TRIM(‘ Tech Agilist Blog ‘) | Tech Agilist Blog |
TRIM(LEADING ‘ ‘ FROM ‘ Tech Agilist Blog ‘ OR LTRIM(‘ Tech Agilist Blog ’) | Tech Agilist Blog |
TRIM(TRAILING ‘ ‘ FROM ‘ Tech Agilist Blog ‘) OR RTRIM(‘ Tech Agilist Blog ’) | Tech Agilist Blog |
TRIM(BOTH ‘ ‘ FROM ‘ Tech Agilist Blog ‘) | Tech Agilist Blog |
TRIM(LEADING ‘XXX’ FROM ‘XXX Tech Agilist Blog XXX’) OR LTRIM(‘XXX Tech Agilist Blog XXX’ , ‘XXX’) | Tech Agilist Blog XXX |
TRIM(TRAILING ‘XXX’ FROM ‘XXX Tech Agilist Blog XXX’) OR RTRIM(‘XXX Tech Agilist Blog XXX’ , ‘XXX’) | XXX Tech Agilist Blog |
TRIM(BOTH ‘XXX’ FROM ‘XXX Tech Agilist Blog XXX’) | Tech Agilist Blog |
Note: The TRIM() function will not remove any characters (whitespaces or user-defined) in between the string.
We can make use of the TRIM() scalar function in DB2 DBMS provided by IBM to remove a particular character or blank space from the start or end of a string. By default, if we don’t mention LEADING or TRAILING while using the function the character is stripped from both sides. Also, if the character to be removed that is strip character is not mentioned, it is considered to be blank space.
A well-maintained product backlog is crucial for successful product development. It serves as a single…
Incremental value to the customer refers to the gradual delivery of small, functional parts of…
A Product Market refers to the group of potential customers who might be interested in…
The Professional Agile Leadership - Evidence-Based Management (PAL-EBM) certification offered by Scrum.org is designed for…
The Professional Agile Leadership (PAL I) certification, offered by Scrum.org, is designed to equip leaders…
Choosing the right Scrum Master Certification depends on your current experience and career goals. If…