Oracle日期格式轉換

2021-08-16 21:17:51 字數 2167 閱讀 8459

本文主要介紹oracle中的日期轉換。

日期轉化為字串 (以2023年10月20日為例)

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')  strdatetime from dual;

--獲取年-月-日 時:分:秒

--顯示結果為:2016-10-20 12:35:21

select to_char(sysdate,'yyyymmdd hh24:mi:ss') strdatetime from dual;

--獲取年月日 時:分:秒

--顯示結果為:20161020 13:39:25

select to_char(sysdate,'yyyymmdd') strdatetime from dual;

--獲取年月日

--顯示結果為:20161020

select to_char(sysdate,'yyyy') stryear from dual;

--獲取年

--顯示結果為:2016

select to_char(sysdate,'mm') strmonth from dual;

--獲取月

--顯示結果為:10

select to_char(sysdate,'dd') strday from dual;

--獲取日

--顯示結果為:20

select to_char(sysdate,'hh24') strhour from dual;

--獲取時

--顯示結果為:13

select to_char(sysdate,'mi') strminute from dual;

--獲取分

--顯示結果為:46

select to_char(sysdate,'ss') strsecond from dual;

--獲取秒

--顯示結果為:43

字串和時間互轉

select to_date('2010-10-20 13:23:44','yyyy-mm-dd hh24:mi:ss') datetime from dual;

--顯示結果:2010/10/20 13:23:44

select to_date('2010-10-20 13:23:44','yyyy/mm/dd hh24:mi:ss') datetime from dual;

--顯示結果:2010/10/20 13:23:44

select to_char( to_date(222,'j'),'jsp') from dual;

--顯示結果:two hundred twenty-two

--如果按照下面的例子寫,會報錯:ora-01849:小時值必須介於1和12之間。(因為其中的hh是12進製,沒有13所以報錯)

select to_date('2005-12-25,13:25:59','yyyy-mm-dd,hh:mi:ss') from dual;

查詢某天是星期幾

select to_char(to_date('2012-10-20','yyyy-mm-dd'),'day') strday from dual;

--顯示結果:星期六

select to_char(to_date('2012-10-20','yyyy-mm-dd'),'day','nls_date_language = english') strday from dual;

--顯示結果:saturday

兩個日期間的天數

select floor(sysdate - to_date('20161010','yyyymmdd')) strtime from dual;

--其中sysdate=2016/10/20 17:10:51

--顯示結果:10

時間為null的用法

select to_date(null) from dual;
月份差

select  months_between(date

'2014-04-23',date

'2013-04-23') days from dual;

ORACLE日期格式轉換

今天乙個朋友,資料庫做了資料遷移,可是在新的oracle資料庫中,卻忘記了設定日期格式為date,而設定成了varchar2,所以在資料庫中顯示的日期格式都是 11 1月 10 現在需要轉換為 yyyy mm dd 格式的,這樣的問題之前還沒有遇到過,oracle有乙個to date 函式,是把字元...

oracle日期格式轉換問題

在預設的sqlplus或者sqlplusw下或者程式編寫過程中,執行該命令後,如果是同乙個session是肯定會產生你要的效果。如果要將整體的資料庫級別預設格式改掉需要修改引數檔案,並重啟資料庫才可以,但是這個也改不掉你第三方工具的顯示格式,第三方工具只能在工具內部去改,它只能改掉預設的日期格式,第...

Oracle實現日期格式轉換

to date 將字元型轉換為date型 select to date 2011 09 20 08 30 45 yyyy mm dd hh24 mi ss from dual to char 將date轉換為字元型 select to char sysdate,yyyy mm dd hh24 mi ...