Oracle trim函數用法詳解

來源:互聯網
上載者:User

今天看Oracle文檔,發現trim函數的用法不僅僅局限於去除字串的空格!特翻閱官方解釋,並最如下實驗,希望大家共同學習!

1. 首先看一下trim函數的文法圖:http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions199.htm

文法描述如下:

TRIM([ { { LEADING | TRAILING | BOTH }
         [ trim_character ]
       | trim_character
       }
       FROM
     ]
     trim_source
    )參數解釋:

leading   開頭字元

trailing    結尾字元

both        開頭和結尾字元

trim_character  去除的字元

trim_source      修剪源

2. 下面進行功能介紹:

trim函數用來去除一個字串的開頭或結尾(或兩者)的字元。

 1)如果指定leading參數,oracle資料庫將去除任何等於trim_character的開頭字元。

例:

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

test_trim
--------------------
dylan

2)如果指定traling參數,oracle將去除任何等於trim_character的結尾字元。

例:

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

test_trim
--------------------
dylan

3)如果指定了both參數或者三個參數都未指定,oracle將去除任何等於trim_character的開頭和結尾字元。

例:

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)如果沒有指定trim_character參數,預設去除的值為空白格。

例:

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

test_trim
--------------------
dylan

5)如果只指定修剪源(trim_source),oracle將去除trim_source的開頭和結尾的空格。

例:

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

test_trim
--------------------
dylan

6)trim函數返回一個varchar2類型值。該值最大的長度等於trim_source的長度。

7)如果trim_source和trim_character有一個為null,則trim函數返回null。

例:

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
--------------------

注意:trim_character和trim_source都可以為以下任意一種資料類型:CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, OR NCLOB。

傳回值的類型與trim_source的資料類型一致。下面是官方的例子:檢索employees表中僱用日期不以0開頭的記錄。

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.