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-809730/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle Date Function 講解和事例【Blog 搬家】OracleFunction
- DATE 相關的FunctionFunction
- JavaScript中的Date,RegExp,Function物件JavaScriptFunction物件
- Oracle Date FunctionsOracleFunction
- 批量提交事例
- Oracle中Date和Timestamp的區別Oracle
- oracle function overviewOracleFunctionView
- Oracle Table FunctionOracleFunction
- oracle function powerOracleFunction
- date_format(date,frm) 詳解ORM
- oracle使用outline固定執行計劃事例Oracle
- oracle to_date格式Oracle
- Oracle date計算Oracle
- oracle INTERNAL_FUNCTIONOracleFunction
- Oracle Deterministic FunctionOracleFunction
- Mysql date_format 與 Oracle to_char(date,’format’)MySqlORMOracle
- mysql master-slave複製錯誤[解決事例]MySqlAST
- Oracle date 型別比較和String比較Oracle型別
- date 命令詳解
- GoldenGate Oracle MSSQL DateGoOracleSQL
- Oracle 11G 程式講解Oracle
- CUUG ORACLE檢查點講解Oracle
- CUUG oracle物化檢視講解Oracle
- Oracle Pipelined FunctionOracleFunction
- oracle function函式castOracleFunction函式AST
- BIRT呼叫oracle function薦OracleFunction
- a demo function of oracle for AKingFunctionOracle
- ORACLE DATE型別和TIMESTAMP型別DUMP換算Oracle型別
- oracle中sysdate和current_date兩者的區別Oracle
- oracle資料型別date和timestamp的轉化Oracle資料型別
- oracle timestamp轉換date及date型別相減Oracle型別
- 使用Oracle to_date方法【轉】Oracle
- Oracle SQL精妙SQL語句講解OracleSQL
- Oracle ADF 應用--案例講解Oracle
- Oracle 中的 TO_DATE 和 TO_CHAR 函式 日期處理Oracle函式
- Oracle基礎之function使用OracleFunction
- oracle create function 例項2OracleFunction
- Oracle Profile and PASSWORD_VERIFY_FUNCTIONOracleFunction