Oracle trim function usage

Source: Internet
Author: User

I read the Oracle document today and found that trim function usage is not limited to removing spaces of strings! Read the official explanation and the following experiment. I hope you can learn it together!

1. first look at the trim function syntax diagram: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions199.htm

The syntax is described as follows:

TRIM ([{LEADING | TRAILING | BOTH}
[Trim_character]
| Trim_character
}
FROM
]
Trim_source
) Parameter explanation:

Leading

Trailing end character

Both start and end characters

Characters removed by trim_character

Trim_source trim Source

2. The following functions are introduced:

The trim function is used to remove the start or end (or both) characters of a string.

1) if the leading parameter is specified, the oracle database removes any starting characters equal to trim_character.

Example:

SQL> select trim (leading 'X' from 'xdylan') "test_trim" from dual;

Test_trim
--------------------
Dylan

2) If the traling parameter is specified, oracle removes any trailing characters equal to trim_character.

Example:

SQL> select trim (trailing 'X' from 'dylanx') "test_trim" from dual;

Test_trim
--------------------
Dylan

3) if the both parameter is specified or none of the three parameters are specified, oracle removes any starting and ending characters equal to trim_character.

Example:

SQL> select trim (both 'X' from 'xdylanx') "test_trim" from dual;

Test_trim
--------------------
Dylan

SQL> select trim ('x' from 'xdylanx') "test_trim" from dual;

Test_trim
--------------------
Dylan

4) if the trim_character parameter is not specified, spaces are removed by default.

Example:

SQL> select trim (both from 'dylan') "test_trim" from dual;

Test_trim
--------------------
Dylan

5) If you only specify the trim source (trim_source), oracle removes spaces at the beginning and end of trim_source.

Example:

SQL> select trim ('dylan') "test_trim" from dual;

Test_trim
--------------------
Dylan

6) The trim function returns a value of the varchar2 type. The maximum length of this value is equal to the length of trim_source.

7) If either trim_source or trim_character is null, the trim function returns null.

Example:

SQL> select trim (trailing null from 'dylan') "test_trim" from dual;

Test_trim
--------------------

 


SQL> select trim (trailing 'X' from null) "test_trim" from dual;

Test_trim
--------------------

Note: Both trim_character and trim_source can belong to any of the following data types: CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or nclob.

The type of the returned value is the same as that of trim_source. The following is an official example: retrieve records with employment dates not starting with 0 in the employees table.

SELECT employee_id,
TO_CHAR (TRIM (LEADING 0 FROM hire_date ))
FROM employees
WHERE department_id = 60;

EMPLOYEE_ID TO_CHAR (T
--------------------
103 3-JAN-90
104 21-MAY-91
105 25-JUN-97
106 5-FEB-98
107 7-FEB-99

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.