http://www.cnblogs.com/kingjiong/archive/2009/02/19/1393815.html
http://www.cnblogs.com/kingjiong/archive/2009/02/19/1393815.html
TO_DATE格式
Day:
dd number 12
dy abbreviated fri
day spelled out friday
ddspth spelled out, ordinal twelfth
Month:
mm number 03
mon abbreviated mar
month spelled out march
Year:
yy two digits 98
yyyy four digits 1998
24小時格式下時間範圍為: 0:00:00 - 23:59:59....
12小時格式下時間範圍為: 1:00:00 - 12:59:59 ....
常用:
select * from payment t
where transaction_date = to_date('18-09-2008', 'dd-mm-yyyy')
select * from payment t
where last_update_date = to_date('18-09-2008 11:59:14', 'dd-mm-yyyy hh24:mi:ss')
where last_update_date = to_date('18-09-2008 11:59:14', 'dd-mm-yyyy hh24:mi:ss')
1. 日期和字元轉換函式用法(to_date,to_char)
2. select to_char( to_date(222,'J'),'Jsp') from dual
顯示Two Hundred Twenty-Two
3. 求某天是星期幾
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;
星期一
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;
monday
設定日期語言 :ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
也可以這樣 :TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')
4. 兩個日期間的天數
select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
5. 時間為null的用法
select id, active_date from table1
UNION
select 1, TO_DATE(null) from dual;
注意要用TO_DATE(null)
6. a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')
那麼12月31號中午12點之後和12月1號的12點之前是不包含在這個範圍之內的。
所以,當時間需要精確的時候,覺得to_char還是必要的
7. 日期格式衝突問題
輸入的格式要看你安裝的ORACLE字符集的型別, 比如: US7ASCII, date格式的型別就是: '01-Jan-01'
alter system set NLS_DATE_LANGUAGE = American
alter session set NLS_DATE_LANGUAGE = American
或者在to_date中寫
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;
注意我這只是舉了NLS_DATE_LANGUAGE,當然還有很多,
可檢視
select * from nls_session_parameters
select * from V$NLS_PARAMETERS
8.
select count(*) from
( select rownum-1 rnum from all_objects
where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002- 02-01','yyyy-mm-dd')+1
)
where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' ) not in ( '1', '7' )
查詢2002-02-28至2002-02-01間除星期一和七的天數
在前後分別呼叫DBMS_UTILITY.GET_TIME, 讓後將結果相減(得到的是1/100秒, 而不是毫秒).
9.
select months_between(to_date('01-31-1999','MM-DD-YYYY'),
to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
1
select months_between(to_date('02-01-1999','MM-DD-YYYY'),
to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
1.03225806451613
where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002- 02-01','yyyy-mm-dd')+1
)
where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' ) not in ( '1', '7' )
查詢2002-02-28至2002-02-01間除星期一和七的天數
在前後分別呼叫DBMS_UTILITY.GET_TIME, 讓後將結果相減(得到的是1/100秒, 而不是毫秒).
9.
select months_between(to_date('01-31-1999','MM-DD-YYYY'),
to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
1
select months_between(to_date('02-01-1999','MM-DD-YYYY'),
to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
1.03225806451613
10. Next_day的用法
Next_day(date, day)
Monday-Sunday, for format code DAY
Mon-Sun, for format code DY
1-7, for format code D
11
select to_char(sysdate,'hh:mi:ss') TIME from all_objects
注意:第一條記錄的TIME 與最後一行是一樣的
可以建立一個函式來處理這個問題
create or replace function sys_date return date is
begin
return sysdate;
end;
select to_char(sys_date,'hh:mi:ss') from all_objects;
12. 獲得小時數
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer
SQL> select sysdate ,to_char(sysdate,'hh') from dual;
SYSDATE TO_CHAR(SYSDATE,'HH')
-------------------- ---------------------
2003-10-13 19:35:21 07
SQL> select sysdate ,to_char(sysdate,'hh24') from dual;
SYSDATE TO_CHAR(SYSDATE,'HH24')
-------------------- -----------------------
2003-10-13 19:35:21 19
獲取年月日與此類似
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer
SQL> select sysdate ,to_char(sysdate,'hh') from dual;
SYSDATE TO_CHAR(SYSDATE,'HH')
-------------------- ---------------------
2003-10-13 19:35:21 07
SQL> select sysdate ,to_char(sysdate,'hh24') from dual;
SYSDATE TO_CHAR(SYSDATE,'HH24')
-------------------- -----------------------
2003-10-13 19:35:21 19
獲取年月日與此類似
13. 年月日的處理
select older_date,
newer_date,
years,
months,
abs(
trunc(
newer_date-
add_months( older_date,years*12+months )
)
) days
from ( select
trunc(months_between( newer_date, older_date )/12) YEARS,
mod(trunc(months_between( newer_date, older_date )),
12 ) MONTHS,
newer_date,
older_date
from ( select hiredate older_date,
add_months(hiredate,rownum)+rownum newer_date
from emp )
)
14. 處理月份天數不定的辦法
select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual
16. 找出今年的天數
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual
閏年的處理方法
to_char( last_day( to_date('02' || :year,'mmyyyy') ), 'dd' )
如果是28就不是閏年
17. yyyy與rrrr的區別
'YYYY99 TO_C
------- ----
yyyy 99 0099
rrrr 99 1999
yyyy 01 0001
rrrr 01 2001
18.不同時區的處理
select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdate
from dual;
19. 5秒鐘一個間隔
Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,TO_CHAR(sysdate,'SSSSS')
from dual
2002-11-1 9:55:00 35786
SSSSS表示5位秒數
20. 一年的第幾天
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual
310 2002-11-6 10:03:51
21.計算小時,分,秒,毫秒
select
Days,
A,
TRUNC(A*24) Hours,
TRUNC(A*24*60 - 60*TRUNC(A*24)) Minutes,
TRUNC(A*24*60*60 - 60*TRUNC(A*24*60)) Seconds,
TRUNC(A*24*60*60*100 - 100*TRUNC(A*24*60*60)) mSeconds
from
(
select
trunc(sysdate) Days,
sysdate - trunc(sysdate) A
from dual
)
select * from tabname
order by decode(mode,'FIFO',1,-1)*to_char(rq,'yyyymmddhh24miss');
//
floor((date2-date1) /365) 作為年
floor((date2-date1, 365) /30) 作為月
mod(mod(date2-date1, 365), 30)作為日.
select older_date,
newer_date,
years,
months,
abs(
trunc(
newer_date-
add_months( older_date,years*12+months )
)
) days
from ( select
trunc(months_between( newer_date, older_date )/12) YEARS,
mod(trunc(months_between( newer_date, older_date )),
12 ) MONTHS,
newer_date,
older_date
from ( select hiredate older_date,
add_months(hiredate,rownum)+rownum newer_date
from emp )
)
14. 處理月份天數不定的辦法
select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual
16. 找出今年的天數
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual
閏年的處理方法
to_char( last_day( to_date('02' || :year,'mmyyyy') ), 'dd' )
如果是28就不是閏年
17. yyyy與rrrr的區別
'YYYY99 TO_C
------- ----
yyyy 99 0099
rrrr 99 1999
yyyy 01 0001
rrrr 01 2001
18.不同時區的處理
select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdate
from dual;
19. 5秒鐘一個間隔
Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,TO_CHAR(sysdate,'SSSSS')
from dual
2002-11-1 9:55:00 35786
SSSSS表示5位秒數
20. 一年的第幾天
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual
310 2002-11-6 10:03:51
21.計算小時,分,秒,毫秒
select
Days,
A,
TRUNC(A*24) Hours,
TRUNC(A*24*60 - 60*TRUNC(A*24)) Minutes,
TRUNC(A*24*60*60 - 60*TRUNC(A*24*60)) Seconds,
TRUNC(A*24*60*60*100 - 100*TRUNC(A*24*60*60)) mSeconds
from
(
select
trunc(sysdate) Days,
sysdate - trunc(sysdate) A
from dual
)
select * from tabname
order by decode(mode,'FIFO',1,-1)*to_char(rq,'yyyymmddhh24miss');
//
floor((date2-date1) /365) 作為年
floor((date2-date1, 365) /30) 作為月
mod(mod(date2-date1, 365), 30)作為日.
23.next_day函式
next_day(sysdate,6)是從當前開始下一個星期五。後面的數字是從星期日開始算起。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/219982/viewspace-619842/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Sqlite 分頁(http://www.cnblogs.com/shang53880/archive/2011/03/11/1981749.html)SQLiteHTTPHiveHTML
- http://www.blogjava.net/javapro/archive/2009/03/24/JavaAppSetup.htmlHTTPJavaHiveAPPHTML
- T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)SQLHTTPHiveHTML
- JS間隔性滾動程式碼[修正增加了部分功能] 【轉:http://www.cnblogs.com/robot/archive/2008/04/09/1144568.html】...JSHTTPHiveHTML
- ORA-16019: cannot use LOG_ARCHIVE_DEST_1 with LOG_ARCHIVE_DESTHive
- 11G新特性:FLASHBACK ARCHIVE – 02Hive
- P1912 [NOI2009] 詩人小G
- 企業案例:查詢當前目錄下所有檔案,並把檔案中的https://www.cnblogs.com/zhaokang2019/字串替換成https://www.cnblogs.com/guobaoyan2019/HTTP字串
- Swift iOS : ArchiveSwiftiOSHive
- 更改Archive ModeHive
- HTTP Archive:全球網頁平均大小超1M,需要針對移動優化HTTPHive網頁優化
- archive log 日誌已滿“ORA-00257: archiver error”HiveError
- ARCHIVE歸檔日誌爆滿故障ORA-00257Hive
- HTTP 錯誤 500.19 - Internal Server ErrorHTTPServerError
- standby_archive_dest和log_archive_dest_n區別Hive
- log_archive_dest與log_archive_dest_n區別Hive
- [20170215]設定log_archive_dest_state_2Hive
- Oracle Flashback Data ArchiveOracleHive
- Flashback Data Archive RequirementsHiveUIREM
- 02 前端HTTP協議(圖解HTTP) 之 簡單的HTTP協議前端HTTP協議圖解
- HTTP 錯誤 500.19 - Internal Server Error v4.0.30319HTTPServerError
- 11g feature: Flashback Data Archive Guide. (Doc ID 470199.1)HiveGUIIDE
- https://www.cnblogs.com/Akko/p/16039879.htmlHTTPHTML
- Ten examples of git-archiveGitHive
- MySQL Archive儲存引擎MySqlHive儲存引擎
- git archive命令詳解GitHive
- oracle archive歸檔初步OracleHive
- 【轉】詳解log_archive_dest與log_archive_dest_n區別Hive
- 關於 log_archive_dest與log_archive_dest_n a的區別Hive
- HTTP Archive:研究顯示目前越來越多的外掛 導致網站載入速度越來越慢HTTPHive網站
- [極客大挑戰 2019]Http wpHTTP
- rfc1945-http1.0自譯本-(4) (轉)HTTP
- rfc1945-http1.0自譯本-(1) (轉)HTTP
- rfc1945-http1.0自譯本-(5) (轉)HTTP
- rfc1945-http1.0自譯本-(3) (轉)HTTP
- rfc1945-http1.0自譯本-(7) (轉)HTTP
- rfc1945-http1.0自譯本-(6) (轉)HTTP
- rfc1945-http1.0自譯本-(2) (轉)HTTP