
SQL TRIM Function Example | trim() Function In SQL is today’s topic. The SQL TRIM function is used for removing spaces and characters from both ends of the string. The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string.
SQL TRIM Function Example
The syntax of TRIM() Function is following.
TRIM ([removed_characters FROM] input_string)
See the parameters of TRIM() function.
- removed_characters: It is a literal, variable of any non-LOB character type contains characters which will be removed.
- input_string: It is an expression that depicts the kind of characters to be removed.
Examples
See the following query.
Select TRIM (‘ SQL TUTORIAL ‘);
See the following output.
SQL TUTORIAL
Here, in the above example leading and trailing whitespaces were removed.
See the following query.
Select TRIM (‘@#’ FROM ‘123@456###’)
See the output.
123456
Here, in the above example, specified characters, i.e. @ and #, are removed from the input_string.
LTRIM and RTRIM are the parts of the SQL TRIM function.
LTRIM(): This function is used for removing whitespaces at the beginning of the string.
Example:
Select LTRIM (‘ Example For LTRIM ‘);
See the output.
Example For LTRIM
RTRIM (): This function is used for removing whitespaces at the end of the string.
See the following query.
Select RTRIM (‘ Example For RTRIM ’);
See the output.
Example For RTRIM
Above were the concepts which work in SQL SERVER.
Let’s see the working of the TRIM function in ORACLE.
See the following syntax.
TRIM ([ [{LEADING | TRAILING | BOTH}] [removal_char] FROM] target_string [COLLATE collation_name])
PARAMETERS
- LEADING: It depicts the leftmost part of the string.
- TRAILING: It depicts the rightmost part of the string.
- BOTH: It depicts both the left and right part of the string.
- Removal_char: It depicts the character to be removed.
- Target_string: It depicts the string onto which action is to be taken.
- Collation_name: It describes the collation to be applied to the string.
See the following query.
Select TRIM (TRAILING ‘1’ FROM 1234567891) FROM dual;
See the output.
123456789
Here, Trailing means rightmost part of the string, so one was deleted from rightmost part of the string.
Let’s apply the TRIM function to a table.
Consider a table Employee:
Emp_id | First_name | City | State | Salary |
101 | Maman | Patna | Bihar | 30000 |
201 | Mavni | Jalandhar | Punjab | 20000 |
301 | Karan | Allahabad | Uttar Pradesh | 40000 |
401 | Suraj | Kolkata | West Bengal | 60000 |
501 | Ravindran | Vizag | Andhra Pradesh | 70000 |
Now, suppose we want to remove the leading ‘m’ from First_name then the following query has to be used. See the following query.
Select TRIM (LEADING ‘m’ from First_name) From Employee;
See the output.
First_name |
Aman |
Avni |
Karan |
Suraj |
Ravindran |
Here, you can see that M was removed from the 1st position.
Let’s remove the trailing ‘n’ from the First_name then the following query has to be used.
See the following query.
Select TRIM (TRAILING ‘n’ from First_name) From Employee;
See the output.
First_name |
Maman |
Mavni |
Kara |
Suraj |
Ravindra |
Here, you can see that N was removed from the last position.
Finally, SQL TRIM Function Example | trim() Function In SQL is over.
Recommended Posts
SQL Substring Function Example | Substring In SQL
SQL CONCAT Function Example | Concat In SQL Tutorial
SQL Replace Function Example | Replace Function In SQL
SQL Check Constraint Example | Check Constraint In SQL Tutorial
SQL String Functions Example | String Functions In SQL
SQL Try Catch Example | Implement Error Handling in SQL
SQL NULL Functions Example | NULL Functions in SQL Tutorial
The post SQL TRIM Function Example | trim() Function In SQL appeared first on AppDividend.