Oracle Date Function 講解和事例【Blog 搬家】
Oracle Date Function 講解和事例
1 year=1*12 months
1 day=24 hours=24*(1*60mins)=24*60*(1*60 seconds)
1 week =7 days
current_timestamp
datimezone
months_between
next_day
round
SYSDATE
trunc()
B:上個月的今天
select add_months(sysdate,-1) from dual --在月份上減少
C: 上個月的最後一天
select last_day(add_months(sysdate,-1)) from dual
--------------- ---------------
20051109 160535 20051109 160534 B:
--------------- ---------------
20051109 030644 20051109 160644
---------------------------------------------------------------------------
09-NOV-05 04.27.41.416811 PM +08:00
B: use precision
SESSIONTIMEZONE CURRENT_TIMESTAMP
-------------------- --------------------------------------------------
-05:00 09-NOV-05 03.33.32.595277 AM -05:00
C: You can user others date funtion on current_timestamp
--------
20051109
-----------------------------------------------
2005
B:
------------------------------------------------
116: Last_day
--------- --------- ----------
10-NOV-05 30-NOV-05 20
-.09677419 -3 (11 月只有30 天)
2 BST, BDT: Bering Standard or Daylight Time
3 CST, CDT: Central Standard or Daylight Time
4 EST, EDT: Eastern Standard or Daylight Time
5 GMT: Greenwich Mean Time
6 HST, HDT: Alaska-Hawaii Standard Time or Daylight Time.
7 MST, MDT: Mountain Standard or Daylight Time
8 NST: Newfoundland Standard Time
9 PST, PDT: Pacific Standard or Daylight Time
10 YST, YDT: Yukon Standard or Daylight Time
------------------- -------------------
2005-11-10 06:24:06 2005-11-10 10:24:06
--------------------------
05-11-14 10:48:01
Format Mask |
Rounds or Truncates to |
---|---|
CC or SSC |
Century |
SYYY, YYYY, YEAR, SYEAR, YYY, YY, or Y |
Year (rounds up to next year on July 1) |
IYYY, IYY, IY, or I |
Standard ISO year |
Q |
Quarter (rounds up on the sixteenth day of the second month of the quarter) |
MONTH, MON, MM, or RM |
Month (rounds up on the sixteenth day, which is not necessarily the same as the middle of the month) |
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 year |
W |
Same day of the week as the first day of the month |
DDD, DD, or J |
Day |
DAY, DY, or D |
Starting day of the week |
HH, HH12, HH24 |
Hour |
MI |
Minute |
Example:
-
Round up to the next century:
TO_CHAR (ROUND (TO_DATE ('01-MAR-1994'), 'CC'), 'DD-MON-YYYY') ==> 01-JAN-2000
-
Round back to the beginning of the current century:
TO_CHAR (ROUND (TO_DATE ('01-MAR-1945'), 'CC'), 'DD-MON-YYYY') ==> 01-JAN-1900
-
Round down and up to the first of the year:
ROUND (TO_DATE ('01-MAR-1994'), 'YYYY') ==> 01-JAN-1994 ROUND (TO_DATE ('01-SEP-1994'), 'YEAR') ==> 01-JAN-1995
-
Round up and down to the quarter (first date in the quarter):
ROUND (TO_DATE ('01-MAR-1994'), 'Q') ==> 01-APR-1994 ROUND (TO_DATE ('15-APR-1994'), 'Q') ==> 01-APR-1994
-
Round down and up to the first of the month:
ROUND (TO_DATE ('12-MAR-1994'), 'MONTH') ==> 01-MAR-1994 ROUND (TO_DATE ('17-MAR-1994'), 'MM') ==> 01-APR-1994
-
Day of first of year is Saturday:
TO_CHAR (TO_DATE ('01-JAN-1994'), 'DAY') ==> 'SATURDAY'
So round to date of nearest Saturday for `01-MAR-1994':
ROUND (TO_DATE ('01-MAR-1994'), 'WW') ==> 26-FEB-1994
-
First day in the month is a Friday:
TO_CHAR (TO_DATE ('01-APR-1994'), 'DAY') ==> FRIDAY
So round to date of nearest Friday from April 16, 1994:
TO_CHAR ('16-APR-1994'), 'DAY') ==> SATURDAY ROUND (TO_DATE ('16-APR-1994'), 'W') ==> 15-APR-1994 TO_CHAR (ROUND (TO_DATE ('16-APR-1994'), 'W'), 'DAY') ==> FRIDAY
使用To_char 和Round 組合顯示日期:
-
Round back to nearest day (time always midnight):
TO_CHAR (ROUND (TO_DATE ('11-SEP-1994 10:00 AM', 'DD-MON-YY HH:MI AM'), 'DD'), 'DD-MON-YY HH:MI AM') ==> 11-SEP-1994 12:00 AM
-
Round forward to the nearest day:
TO_CHAR (ROUND (TO_DATE ('11-SEP-1994 4:00 PM', 'DD-MON-YY HH:MI AM'), 'DD'), 'DD-MON-YY HH:MI AM') ==> 12-SEP-1994 12:00 AM
-
Round back to the nearest hour:
TO_CHAR (ROUND (TO_DATE ('11-SEP-1994 4:17 PM', 'DD-MON-YY HH:MI AM'), 'HH'), 'DD-MON-YY HH:MI AM') ==> 11-SEP-1994 04:00 PM
FROM DUAL;
本月的第一天
select trunc(sysdate,'month') from dual
select trunc(sysdate,'year') from dual
本月的最後一天
select last_day(sysdate) from dual
本週的第一天
select trunc(sysdate,'day') from dual --Oracle default start week is Sunday
select trunc(sysdate,'iw') from dual ---ISO year default start week is Monday
本週的星期一
select trunc(sysdate,'day')+1 from dual --2,3,4,5,6,
經常會用到的一些Trunc 函式例子(預設的日期格式是DD-MON-YYYY);
-
Without a format mask, TRUNC sets the time to 12:00 A.M. of the same day:
TO_CHAR (TRUNC (TO_DATE ('11-SEP-1994 9:36 AM', 'DD-MON-YYYY HH:MI AM')) ==> 11-SEP-1994 12:00 AM
-
Trunc to the beginning of the century in all cases:
TO_CHAR (TRUNC (TO_DATE ('01-MAR-1994'), 'CC'), 'DD-MON-YYYY') ==> 01-JAN-1900 TO_CHAR (TRUNC (TO_DATE ('01-MAR-1945'), 'CC'), 'DD-MON-YYYY') ==> 01-JAN-1900
-
Trunc to the first of the current year:
TRUNC (TO_DATE ('01-MAR-1994'), 'YYYY') ==> 01-JAN-1994 TRUNC (TO_DATE ('01-SEP-1994'), 'YEAR') ==> 01-JAN-1994
-
Trunc to the first day of the quarter:
TRUNC (TO_DATE ('01-MAR-1994'), 'Q') ==> 01-JAN-1994 TRUNC (TO_DATE ('15-APR-1994'), 'Q') ==> 01-APR-1994
-
Trunc to the first of the month:
TRUNC (TO_DATE ('12-MAR-1994'), 'MONTH') ==> 01-MAR-1994 TRUNC (TO_DATE ('17-MAR-1994'), 'MM') ==> 01-APR-1994
TO_Char 函式和Trunc 函式的一些組合:
-
Trunc back to the beginning of the current day (time is always midnight):
TO_CHAR (TRUNC (TO_DATE ('11-SEP-1994 10:00 AM', 'DD-MON-YYYY HH:MI AM'), 'DD'), 'DD-MON-YYYY HH:MI AM') ==> 11-SEP-1994 12:00 AM TO_CHAR (TRUNC (TO_DATE ('11-SEP-1994 4:00 PM', 'DD-MON-YYYY HH:MI AM'), 'DD'), 'DD-MON-YYYY HH:MI AM') ==> 11-SEP-1994 12:00 AM
-
Trunc to the beginning of the current hour:
TO_CHAR (TRUNC (TO_DATE ('11-SEP-1994 4:17 PM', 'DD-MON-YYYY HH:MI AM'), 'HH'), 'DD-MON-YYYY HH:MI AM') ==> 11-SEP-1994 04:00 PM
------
+00:00
YEAR
MONTH
DAY
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/34596/viewspace-661937/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JavaScript中的Date,RegExp,Function物件JavaScriptFunction物件
- Oracle中Date和Timestamp的區別Oracle
- oracle使用outline固定執行計劃事例Oracle
- Oracle date 型別比較和String比較Oracle型別
- Good site on Oracle tech blogGoOracle
- Oracle SQL精妙SQL語句講解OracleSQL
- Oracle vs PostgreSQL Develop(28) - function overloadOracleSQLdevFunction
- Oracle vs PostgreSQL Develop(24) - subquery with agg functionOracleSQLdevFunction
- js基礎–Date.parse()與Date.getTime()方法詳解JS
- java:Date和DateTime區別Java
- java -- Stringbuild、Date和Calendar類JavaUI
- 部落格搬家
- 搬家成功了!
- 搬家到掘金
- JVM原理講解和調優JVM
- 字首和的基礎講解
- Java中棧和堆講解Java
- Javaparse包的使用和講解Java
- 大白話講解IOC和AOP
- input delay和output delay講解
- java.util.Date和java.sql.Date有什麼區別?JavaSQL
- Java String和Date的轉換Java
- JavaScript內部物件和Date物件JavaScript物件
- javascript 中function(){},new function(),new Function(),Function 摘錄JavaScriptFunction
- 簡書搬家啦!
- 圖靈搬家啦!圖靈
- Oracle資料庫搬家牽扯出的一些知識點記錄Oracle資料庫
- 淺醉和的基礎講解
- git的使用命令和講解Git
- 帝國CMS搬家常見錯誤及解決方法
- Function和Object 應該知道的FunctionObject
- function和bound method的區別Function
- Date
- Laravel Resource Routes和API Resource Routes講解LaravelAPI
- 防抖和節流(例項講解)
- 搬家到雲棲啦
- Git『Everything up-to-date』問題解決Git
- Function.prototype.call.apply作用詳解FunctionAPP
- error: use of deleted function ‘YYSTYPE::YYSTYPE()’[解決]ErrordeleteFunction