Oracle EXTRACT()函數與to_char() 函數

來源:互聯網
上載者:User

轉自:http://www.cnblogs.com/ruanbl/archive/2008/02/29/1086398.html

EXTRACT extracts and returns the value of a specified datetime field from a datetime or interval value expression.
The field you are extracting must be a field of the datetime_value_expr or interval_value_expr. For example, you can extract only YEAR, MONTH, and DAY from a DATE value. Likewise, you can extract TIMEZONE_HOUR and TIMEZONE_MINUTE only from the TIMESTAMP WITH TIME ZONE datatype.
eg.

  • SELECT EXTRACT(YEAR FROM SYSDATE) FROM DUAL; return Current Year
  • SELECT EXTRACT(MONTH FROM SYSDATE) FROM DUAL; return Current Month
  • SELECT EXTRACT(DAY FROM SYSDATE) FROM DUAL; return Current Day
  • SELECT EXTRACT(HOUR FROM SYSDATE) FROM DUAL;Error

       Correct:SELECT EXTRACT(HOUR FROM TIMESTAMP '2005-10-10 10:10:10') FROM DUAL;

  • SELECT EXTRACT(MINUTE FROM SYSDATE) FROM DUAL;Error

       Correct:SELECT EXTRACT(MINUTE FROM TIMESTAMP '2005-10-10 10:10:10') FROM DUAL;

       以上是EXTRACT()函數的使用說明,英文部分有個很重要的部分.就是extract 抽取年月日部分和時分部分的使用方法是不同的.
EXTRACT(YEAR FROM SYSDATE)中FROM 的下一個參數是日期類型的時候可以抽取年月日.就是說這個參數是日期類型的時候就可以抽取年\月\日,而抽取不了時\分部分.抽取時分部分得在FROM 後面加帶TIMESTAMP 且後面加時間字元.
   這裡要注意,在(MINUTE FROM TIMESTAMP '2005-10-10 10:10:10')中,如果把'2005-10-10 10:10:10'用日期類型的欄位來代替並轉換成字元類型後,也是錯誤的,估計是這方法的問題.
    比如(MINUTE FROM TIMESTAMP to_date(to_char(日期類型,'YYYY-MM-DD hh24:mi:ss'),'YYYY-MM-DD hh24:mi:ss')),這樣執行不了的.

    如果真要從日期類型的欄位中抽取時\分部分用to_char() 函數就可以了.比如to_char(日期類型,'hh24:mi') 

    完畢!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.