天天看點

iyyy、yyyy、ww、iw之間的差別及注意的地方

首先看一段來自官網的資訊

SYYYY
YYYY
YEAR
SYEAR
YYY
YY
Y      
Year (rounds up on July 1)
IYYY
IY
IY
I      
ISO Year
Q      
Quarter (rounds up on the sixteenth day of the second month of the quarter)
MONTH
MON
MM
RM      
Month (rounds up on the sixteenth day)
WW      
Same day of the week as the first day of the year
IW      
Same day of the week as the first day of the ISO week, which is Monday
W      
Same day of the week as the first day of the month
DDD
DD
J      
Day
DAY
DY
D      
Starting day of the week
HH
HH12
HH24      
Hour
MI      
Minute

在資料庫的開發當中經常要對一些日期進行轉換處理,比如取某年、某月,或者某周,每年的第一周的第一天等等,這種情況下就必須對orcale日期轉換類型相當的熟悉。

下面詳細介紹兩組類型yyyy和iyyyy,ww和iw。

      經檢視官網可知IYYY、IW指的是ISO的标準格式,而YYYY、WW是普通的日期格式,他們之間的差別在ISO标準認為日期是從周一到周日,按周計算,如果某一年的一月一号是周六或者周日,則某一年的第一周是延續上一年的周結束後算起,如果某一年的一月一号周二三四五,則該年的第一周從去年開始算起。而普通的标準則指定任何一年的一月一号都是周一,是以在使用日期的時候需要特别的注意,具體案列分析如下:

SQL> select to_char(date '2013-12-29','iyyy-mm'),to_char(date '2013-12-30','iyyy-mm'),to_char(date'2013-12-31','iyyy-mm') from dual;

TO_CHAR(DATE'2013-12-29','IYYY TO_CHAR(DATE'2013-12-30','IYYY TO_CHAR(DATE'2013-12-31','IYYY

------------------------------ ------------------------------ ------------------------------

2013-12                        2014-12                        2014-12

Executed in 0.015 seconds

SQL> select to_char(date '2013-12-29','day'),to_char(date '2013-12-30','day'),to_char(date'2013-12-31','day'),to_char(date'2014-01-01','day') from dual;

TO_CHAR(DATE'2013-12-29','DAY' TO_CHAR(DATE'2013-12-30','DAY' TO_CHAR(DATE'2013-12-31','DAY' TO_CHAR(DATE'2014-01-01','DAY'

------------------------------ ------------------------------ ------------------------------ ------------------------------

星期日                         星期一                         星期二                         星期三

Executed in 0.015 seconds 

SQL>  select to_char(date '2013-12-29','yyyy-mm'),to_char(date '2013-12-30','yyyy-mm'),to_char(date'2013-12-31','yyyy-mm') from dual;

TO_CHAR(DATE'2013-12-29','YYYY TO_CHAR(DATE'2013-12-30','YYYY TO_CHAR(DATE'2013-12-31','YYYY

------------------------------ ------------------------------ ------------------------------

2013-12                        2013-12                        2013-12

Executed in 0.015 seconds

 通過前兩上2個sql,可以知道to_char(date '2013-12-30','iyyy-mm'),to_char(date '2013-12-31','iyyy-mm')求出的年月是2014-12月,而29号是2013-12月,這裡并不是說得出的結果是錯誤的,而是oracle是以ISO标準計算值而導緻沒有達到的預期結果。相對應的,第三條sql的值就沒有變成2014-12,而是2013-12。是以在使用的過程中要特别注意iyyy的使用。

其次再看下周數

SQL> select to_char(date '2013-12-29','iyyy-iw'),to_char(date '2013-12-30','iyyy-iw'),to_char(date'2013-12-31','iyyy-iw') from dual;

TO_CHAR(DATE'2013-12-29','IYYY TO_CHAR(DATE'2013-12-30','IYYY TO_CHAR(DATE'2013-12-31','IYYY

------------------------------ ------------------------------ ------------------------------

2013-52                        2014-01                        2014-01

Executed in 0.015 seconds

SQL> select to_char(date '2013-12-29','yyyy-ww'),to_char(date '2013-12-30','yyyy-ww'),to_char(date'2013-12-31','yyyy-ww'),to_char(date'2014-01-01','yyyy-ww') from dual;

TO_CHAR(DATE'2013-12-29','YYYY TO_CHAR(DATE'2013-12-30','YYYY TO_CHAR(DATE'2013-12-31','YYYY TO_CHAR(DATE'2014-01-01','YYYY

------------------------------ ------------------------------ ------------------------------ ------------------------------

2013-52                        2013-52                        2013-53                        2014-01

Executed in 0.031 seconds

SQL> select to_char(date '2013-12-29','day'),to_char(date '2013-12-30','day'),to_char(date'2013-12-31','day'),to_char(date'2014-01-01','day') from dual;

TO_CHAR(DATE'2013-12-29','DAY' TO_CHAR(DATE'2013-12-30','DAY' TO_CHAR(DATE'2013-12-31','DAY' TO_CHAR(DATE'2014-01-01','DAY'

------------------------------ ------------------------------ ------------------------------ ------------------------------

星期日                         星期一                         星期二                         星期三

 通過以上3個sql可以得知采用ww求周數時,周數和星期天數是對應不上的,且周數之間不存在連貫性,反觀采用iw時,2013年和2014年交接時周數是連着的,且于星期數對應上,一般在開發過程中采用iw來求周數,而不是ww。

繼續閱讀