Oracle日期格式化以及extract函式的使用

風靈使發表於2018-06-30

由於業務需要,這兩天在學習Oracle,發現Oracle裡面的日期它會給你轉成一種很不習慣的格式,於是想著怎麼樣把它弄成年、月、日的格式來顯示,查資料、看文件,最終找到解決辦法了,其實是用到了to_char方法。

例如,在Oracle裡面,一個表格裡日期如2017-05-06,列名為time,查詢後發現返回的是06-5月-17這種格式的,看著賊不爽,要想把它轉成年月日這種格式的,可以這樣做,

to_char(time, 'YYYY-MM-DD') as time  // 括號裡的time表示表中的列名,第二個`time`則表示轉換後的日期列名仍然為`time`

現在轉換後的日期就是這樣的,2017-05-06

那麼extract函式是幹什麼的呢?extract英語意思是提取、選取,顧名思義,它表示從一個date型別中擷取某一特定的部分,例如,選取年或月或日。

例如有這樣一個表格:
這裡寫圖片描述

現在我要從表格myTable中選取time中年份為2018年的所有資料,可以這樣做,

select title,play,time from myTable where extract(year from time) = 2018;

或者:

select title,play,to_char(time, 'YYYY-MM-DD') as time from myTable where extract(year from time) = 2018

結果顯然是都返回了(這裡只是做個演示)

現在我要從表格myTable中選取time中月份為5的所有資料,操作為:

select title,play,time from myTable extract(month from time) = 5;

或者:

select title,play,to_char(time, 'YYYY-MM-DD') as time from myTable where extract(month from time) = 5

從表格myTable中選取time中日期為6的所有資料,操作為:

select title,play,time from myTable extract(day from time) = 6;

或者:

slect title,play,to_char(time, 'YYYY-MM-DD') as time from myTable where extract(day from time) = 6;

語法如下:extract(year|month|day|hour|minute|second from column_name) = value


Oracle extract函式用法

//oracle中extract()函式從oracle 9i中引入,用於從一個date或者interval型別中擷取到特定的部分   
//語法如下:   
EXTRACT (   
        { YEAR | MONTH | DAY | HOUR | MINUTE | SECOND }   
        | { TIMEZONE_HOUR | TIMEZONE_MINUTE }   
        | { TIMEZONE_REGION | TIMEZONE_ABBR }   
FROM { date_value | interval_value } )   
//我們只可以從一個date型別中擷取 year,month,day(date日期的格式為yyyy-mm-dd);   
//我們只可以從一個 timestamp with time zone 的資料型別中擷取TIMEZONE_HOUR和TIMEZONE_MINUTE;   
select extract(year from date'2011-05-17') year from dual;   
      YEAR   
----------   
      2011   
select extract(month from date'2011-05-17') month from dual;   
     MONTH   
----------   
         5   
select extract(day from date'2011-05-17') day from dual;   
       DAY   
----------   
        17   
//獲取兩個日期之間的具體時間間隔,extract函式是最好的選擇   
select extract(day from dt2-dt1) day   
      ,extract(hour from dt2-dt1) hour   
      ,extract(minute from dt2-dt1) minute   
      ,extract(second from dt2-dt1) second   
from (   
     select to_timestamp('2011-02-04 15:07:00','yyyy-mm-dd hh24:mi:ss') dt1   
           ,to_timestamp('2011-05-17 19:08:46','yyyy-mm-dd hh24:mi:ss') dt2   
     from dual)   
/   
       DAY       HOUR     MINUTE     SECOND   
---------- ---------- ---------- ----------   
       102          4          1         46   
select extract(year from systimestamp) year   
      ,extract(month from systimestamp) month   
      ,extract(day from systimestamp) day   
      ,extract(minute from systimestamp) minute   
      ,extract(second from systimestamp) second   
      ,extract(timezone_hour from systimestamp) th   
      ,extract(timezone_minute from systimestamp) tm   
      ,extract(timezone_region from systimestamp) tr   
      ,extract(timezone_abbr from systimestamp) ta   
from dual   
/   
     YEAR      MONTH        DAY     MINUTE     SECOND         TH         TM  TR          TA   
---------- ---------- ---------- ---------- ---------- ---------- ---------- --------- ----------   
      2011          5         17          7     14.843         8         0   UNKNOWN     UNK   

oracleextract()函式—-用於擷取年、月、日、時、分、秒

oracleextract()函式從oracle 9i中引入,用於從一個date或者interval型別中擷取到特定的部分

語法如下:

extract (    

        { year | month | day | hour | minute | second }    

        | { timezone_hour | timezone_minute }    

        | { timezone_region | timezone_abbr }    

from { date_value | interval_value } )

只可以從一個date型別中擷取年月日

SQL> select  extract (year from sysdate) year, extract (month from sysdate) month, extract (day from sysdate) day from  dual;

      YEAR      MONTH        DAY
---------- ---------- ----------
      2015          5          4

只可以從一個date型別中擷取年月日

SQL> select extract (year from date '2015-05-04') year, extract (month from date'2015-05-04') month, extract (day from date '2011-05-04') day from dual;

      YEAR      MONTH        DAY
---------- ---------- ----------
      2015          5          4

timestamp中獲取年月日時分秒

select 
 extract(year from systimestamp) year
,extract(month from systimestamp) month
,extract(day from systimestamp) day
,extract(minute from systimestamp) minute
,extract(second from systimestamp) second
,extract(timezone_hour from systimestamp) th
,extract(timezone_minute from systimestamp) tm
,extract(timezone_region from systimestamp) tr
,extract(timezone_abbr from systimestamp) ta
from dual

image

獲取兩個日期之間的具體時間間隔

獲取兩個日期之間的具體時間間隔,extract函式是最好的選擇

select
extract (day from dt2 - dt1) day,
extract (hour from dt2 - dt1) hour,
extract (minute from dt2 - dt1) minute,
extract (second from dt2 - dt1) second
from
(
select
to_timestamp ('2011-02-04 15:07:00','yyyy-mm-dd hh24:mi:ss') dt1,
to_timestamp ('2011-05-17 19:08:46','yyyy-mm-dd hh24:mi:ss') dt2
from
dual
)

image

獲取interval型別特定部分

select extract(year from interval '21' year) year from dual

Interval 語法

interval '{ integer | integer time_expr | time_expr }' 

{ { day | hour | minute } [ ( leading_precision ) ] 

| second [ ( leading_precision [, fractional_seconds_precision ] ) ] } 

[ to { day | hour | minute | second [ (fractional_seconds_precision) ] } ]

leading_precision值的範圍是0到9, 預設是2. time_expr的格式為:hh[:mi[:ss[.n]]] or mi[:ss[.n]] or ss[.n], n表示微秒.

範圍值:

hour: 0 to 23

minute: 0 to 59

second: 0 to 59.999999999

eg:

interval '4 5:12:10.222' day to second(3)               表示: 45小時1210.222秒

interval '4 5:12' day to minute                         表示: 45小時12分

interval '400 5' day(3) to hour                         表示: 4005小時, 4003為精度,所以"day(3)", 注意預設值為2.

interval '400' day(3)                                   表示: 400天

interval '11:12:10.2222222' hour to second(7)           表示: 11小時1210.2222222秒

interval '11:20' hour to minute                         表示: 11小時20分

interval '10' hour                                      表示: 10小時

interval '10:22' minute to second                       表示: 1022秒

interval '10' minute                                    表示: 10分

interval '4' day                                        表示: 4天

interval '25' hour                                      表示: 25小時

interval '40' minute                                    表示: 40分

interval '120' hour(3)                                  表示: 120小時

interval '30.12345' second(2,4)                         表示: 30.1235秒, 因為該地方秒的後面精度設定為4, 要進行四捨五入.

interval '20' day - interval '240' hour = interval '10-0' day to second 表示: 20天 - 240小時 = 100

相關文章