SQL TRIM() function
TRIM() function
The SQL TRIM() removes leading and trailing characters(or both) from a character string.
Syntax:
TRIM( [ [{LEADING | TRAILING | BOTH}] [removal_char] FROM ] target_string [COLLATE collation_name])
PostgreSQL and Oracle
All of above platforms support the SQL syntax of TRIM().
Parameters:
Name | Description |
---|---|
LEADING | Right most position of a string. |
TRAILING | Left most position of a string. |
BOTH | Right and left most position of a string. |
removal_char | Character to be removed. |
target_string | String on which the action will take place. |
collation_name | Name of the collation to be applied to the expression. |
MySQL Syntax:
TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str); TRIM([remstr FROM] str)
Pictorial Presentation :
Application of TRIM()
In the subsequent pages, we have discussed how to apply TRIM() with various SQL clauses. we have used Oracle 10g Express Edition.
Example:
To remove the right most '1' from '1234567896541' from the DUAL table, the following SQL statement can be used :
SELECT TRIM(TRAILING '1' FROM 1234567896541)
AS TRAILING_TRIM
FROM dual;
In the above example, the "TRAILING_TRIM" is a column alias which will come as a column heading to the output.
Output:
TRAILING_TRIM ------------ 123456789654
SQL TRIM() with trailing character
Sample table : customer
To remove right most 'n' from the 'cust_name' column of 'customer' table, the following SQL statement can be used :
SELECT TRIM(TRAILING 'n' FROM cust_name)
FROM customer;
Output
TRIM(TRAILING'N'FROMCUST_NAME) ----------------------------------- Holmes Micheal Albert Ravindra Cook Stuart Bolt Fleming Jacks Yearannaidu Sasikant Ramanatha Avinash Winsto Karl Shilto Charles Srinivas Steve Karolina Marti Ramesh Rangarappa Venkatpati Sundariya
SQL TRIM() with leading character
To remove the left most '1' from the string '1234567896541' from the DUAL table, the following SQL statement can be used :
SQL Code:
SELECT TRIM(LEADING '1' FROM 1234567896541 )
AS LEADING_TRIM
FROM dual;
In the above example, the "LEADING_TRIM" is a column alias which will come as a column heading to the output.
Output:
LEADING_TRIM ------------ 234567896541
SQL TRIM() on column with leading character
Sample table: customer
To remove left most 'S' from the 'cust_name' column of the 'customer' table, the following SQL statement can be used :
SELECT TRIM(LEADING 'S' FROM cust_name )
FROM customer;
Output:
TRIM(LEADING'S'FROMCUST_NAME) -------------------------------- Holmes Micheal Albert Ravindran Cook tuart Bolt Fleming Jacks Yearannaidu asikant Ramanathan Avinash Winston Karl hilton Charles rinivas teven Karolina Martin Ramesh Rangarappa Venkatpati undariya
SQL TRIM() from both side
To remove the left and right most '1' from the string '1234567896541' from the DUAL table, the following SQL statement can be used:
SQL Code:
SELECT TRIM('1' FROM 1234567896541)
AS BOTH_TRIM
FROM dual;
In the above example, the "BOTH_TRIM" is a column alias which will come as a column heading to the output.
Output
BOTH_TRIM ----------- 23456789654
Here is a new document which is a collection of questions with short and simple answers, useful for learning SQL as well as for interviews.
Practice SQL Exercises
- SQL Exercises, Practice, Solution
- SQL Retrieve data from tables [33 Exercises]
- SQL Boolean and Relational operators [12 Exercises]
- SQL Wildcard and Special operators [22 Exercises]
- SQL Aggregate Functions [25 Exercises]
- SQL Formatting query output [10 Exercises]
- SQL Quering on Multiple Tables [8 Exercises]
- FILTERING and SORTING on HR Database [38 Exercises]
- SQL JOINS
- SQL SUBQUERIES
- SQL Union[9 Exercises]
- SQL View[16 Exercises]
- SQL User Account Management [16 Exercise]
- Movie Database
- BASIC queries on movie Database [10 Exercises]
- SUBQUERIES on movie Database [16 Exercises]
- JOINS on movie Database [24 Exercises]
- Soccer Database
- Introduction
- BASIC queries on soccer Database [29 Exercises]
- SUBQUERIES on soccer Database [33 Exercises]
- Hospital Database
- Employee Database
- More to come!
Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.
SQL: Tips of the Day
SQL Server SELECT into existing table.
INSERT INTO dbo.TABLETWO SELECT col1, col2 FROM dbo.TABLEONE WHERE col3 LIKE @search_key
This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:
INSERT INTO dbo.TABLETWO (col1, col2) SELECT col1, col2 FROM dbo.TABLEONE WHERE col3 LIKE @search_key
Database: SQL Server
Ref: https://bit.ly/3y6tpA3
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework