Oracle Trim Function

Oracle 2013. 11. 15. 18:39

TRIM

Syntax

Description of trim.gif follows
Description of the illustration trim.gif

Purpose

TRIM enables you to trim leading or trailing characters (or both) from a character string. If trim_character or trim_source is a character literal, then you must enclose it in single quotation marks.

  • If you specify LEADING, then Oracle Database removes any leading characters equal to trim_character.

  • If you specify TRAILING, then Oracle removes any trailing characters equal to trim_character.

  • If you specify BOTH or none of the three, then Oracle removes leading and trailing characters equal to trim_character.

  • If you do not specify trim_character, then the default value is a blank space.

  • If you specify only trim_source, then Oracle removes leading and trailing blank spaces.

  • The function returns a value with datatype VARCHAR2. The maximum length of the value is the length of trim_source.

  • If either trim_source or trim_character is null, then the TRIM function returns null.

Both trim_character and trim_source can be VARCHAR2 or any datatype that can be implicitly converted to VARCHAR2. The string returned is of VARCHAR2 datatype if trim_source is a character datatype and a LOB if trim_source is a LOB datatype. The return string is in the same character set as trim_source.

Examples

This example trims leading zeros from the hire date of the employees in the hr schema:

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

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


LTRIM

Syntax

Description of ltrim.gif follows
Description of the illustration ltrim.gif

Purpose

LTRIM removes from the left end of char all of the characters contained in set. If you do not specify set, then it defaults to a single blank. If char is a character literal, then you must enclose it in single quotation marks. Oracle Database begins scanning char from its first character and removes all characters that appear in set until reaching a character not in set and then returns the result.

Both char and set can be any of the datatypes CHARVARCHAR2NCHARNVARCHAR2CLOB, or NCLOB. The string returned is of VARCHAR2 datatype if char is a character datatype, NVARCHAR2 if char is a national character datatype, and a LOB if char is a LOB datatype.


Examples

The following example trims the redundant first word from a group of product names in the oe.products table:

SELECT product_name, LTRIM(product_name, 'Monitor ') "Short Name"
   FROM products
   WHERE product_name LIKE 'Monitor%';

PRODUCT_NAME         Short Name
-------------------- ---------------
Monitor 17/HR        17/HR
Monitor 17/HR/F      17/HR/F
Monitor 17/SD        17/SD
Monitor 19/SD        19/SD
Monitor 19/SD/M      19/SD/M
Monitor 21/D         21/D
Monitor 21/HR        21/HR
Monitor 21/HR/M      21/HR/M
Monitor 21/SD        21/SD
Monitor Hinge - HD   Hinge - HD
Monitor Hinge - STD  Hinge - STD

RTRIM

Syntax

Description of rtrim.gif follows
Description of the illustration rtrim.gif

Purpose

RTRIM removes from the right end of char all of the characters that appear in set. This function is useful for formatting the output of a query.

If you do not specify set, then it defaults to a single blank. If char is a character literal, then you must enclose it in single quotation marks. RTRIM works similarly to LTRIM.

Both char and set can be any of the datatypes CHARVARCHAR2NCHARNVARCHAR2CLOB, or NCLOB. The string returned is of VARCHAR2 datatype if char is a character datatype, NVARCHAR2 if expr1 is a national character datatype, and a LOB if char is a LOB datatype.

Examples

The following example trims all the right-most occurrences of period, slash, and equal sign from a string:

SELECT RTRIM('BROWNING: ./=./=./=./=./=.=','/=.') "RTRIM example" FROM DUAL;
 
RTRIM exam
----------
BROWNING:

reference : Oracle® Database SQL Language Reference - Oracle11gR1


그동안 너무 RTRM과 LTRIM만 사용한듯.... 

:     

TISTORY에 Login하려면 여기를 누르세요.