在oracle中處理日期大全

在oracle中處理日期大全

??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 ....??
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??
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??

獲取年月日與此類似??
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)作為日.??
23.next_day函數??
next_day(sysdate,6)是從當前開始下一個星期五。后面的數字是從星期日開始算起。??
1 2 3 4 5 6 7??
日 一 二 三 四 五 六

?

oracle中有很多關于日期的函數

在oracle中有很多關于日期的函數,如:
1、add_months()用于從一個日期值增加或減少一些月份
date_value:=add_months(date_value,number_of_months)
例:
SQL> select add_months(sysdate,12) "Next Year" from dual;

Next Year
----------
13-11月-04

SQL> select add_months(sysdate,112) "Last Year" from dual;

Last Year
----------
13-3月 -13

SQL>

2、current_date()返回當前會放時區中的當前日期
date_value:=current_date
SQL> column sessiontimezone for a15
SQL> select sessiontimezone,current_date from dual;

SESSIONTIMEZONE CURRENT_DA
--------------- ----------
+08:00 13-11月-03

SQL> alter session set time_zone='-11:00'
??2 /

會話已更改。

SQL> select sessiontimezone,current_timestamp from dual;

SESSIONTIMEZONE CURRENT_TIMESTAMP
--------------- ------------------------------------
-11:00 12-11月-03 04.59.13.668000 下午 -11:
? ?? ?? ?? ?? ? 00

SQL>

3、current_timestamp()以timestamp with time zone數據類型返回當前會放時區中的當前日期
timestamp_with_time_zone_value:=current_timestamp([timestamp_precision])
SQL> column sessiontimezone for a15
SQL> column current_timestamp format a36
SQL> select sessiontimezone,current_timestamp from dual;

SESSIONTIMEZONE CURRENT_TIMESTAMP
--------------- ------------------------------------
+08:00 13-11月-03 11.56.28.160000 上午 +08:
? ?? ?? ?? ?? ? 00

SQL> alter session set time_zone='-11:00'
??2 /

會話已更改。

SQL> select sessiontimezone,current_timestamp from dual;

SESSIONTIMEZONE CURRENT_TIMESTAMP
--------------- ------------------------------------
-11:00 12-11月-03 04.58.00.243000 下午 -11:
? ?? ?? ?? ?? ? 00

SQL>

4、dbtimezone()返回時區
varchar_value:=dbtimezone
SQL> select dbtimezone from dual;

DBTIME
------
-07:00

SQL>

5、extract()找出日期或間隔值的字段值
date_value:=extract(date_field from [datetime_value|interval_value])
SQL> select extract(month from sysdate) "This Month" from dual;

This Month
----------
? ?? ???11

SQL> select extract(year from add_months(sysdate,36)) "3 Years Out" from dual;

3 Years Out
-----------
? ?? ? 2006

SQL>

6、last_day()返回包含了日期參數的月份的最后一天的日期
date_value:=last_day(date_value)
SQL> select last_day(date'2000-02-01') "Leap Yr?" from dual;

Leap Yr?
----------
29-2月 -00

SQL> select last_day(sysdate) "Last day of this month" from dual;

Last day o
----------
30-11月-03

SQL>

7、localtimestamp()返回會話中的日期和時間
timestamp_value:=localtimestamp
SQL> column localtimestamp format a28
SQL> select localtimestamp from dual;

LOCALTIMESTAMP
----------------------------
13-11月-03 12.09.15.433000
下午

SQL> select localtimestamp,current_timestamp from dual;

LOCALTIMESTAMP CURRENT_TIMESTAMP
---------------------------- ------------------------------------
13-11月-03 12.09.31.006000 13-11月-03 12.09.31.006000 下午 +08:
下午 00

SQL> alter session set time_zone='-11:00';

會話已更改。

SQL> select localtimestamp,to_char(sysdate,'DD-MM-YYYY HH:MI:SS AM') "SYSDATE" from dual;

LOCALTIMESTAMP SYSDATE
---------------------------- ------------------------
12-11月-03 05.11.31.259000 13-11-2003 12:11:31 下午
下午

SQL>

8、months_between()判斷兩個日期之間的月份數量
number_value:=months_between(date_value,date_value)
SQL> select months_between(sysdate,date'1971-05-18') from dual;

MONTHS_BETWEEN(SYSDATE,DATE'1971-05-18')
----------------------------------------
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?389.855143

SQL> select months_between(sysdate,date'2001-01-01') from dual;

MONTHS_BETWEEN(SYSDATE,DATE'2001-01-01')
----------------------------------------
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?34.4035409

SQL>

9、next_day()給定一個日期值,返回由第二個參數指出的日子第一次出現在的日期值(應返回相應日子的名稱字符串)

?

與周相關日期函數

1.查詢某周的第一天
select trunc(decode(ww, 53, to_date(yy || '3112', 'yyyyddmm'), to_date(yy || '-' || to_char(ww * 7), 'yyyy-ddd')), 'd') last_day
from (select substr('2004-32', 1, 4) yy, to_number(substr('2004-32', 6)) ww
? ?? ?? ?from dual)

select trunc(to_date(substr('2003-01',1,5)||to_char((to_number(substr('2003-01',6)))*7),'yyyy-ddd'),'d')-6 first_day from dual

select min(v_date) from
??(select (to_date('200201','yyyymm') + rownum) v_date
??from all_tables
??where rownum < 370)
where to_char(v_date,'yyyy-iw') = '2002-49'

2.查詢某周的最后一天
select trunc(decode(ww, 53, to_date(yy || '3112', 'yyyyddmm'), to_date(yy || '-' || to_char(ww * 7), 'yyyy-ddd')), 'd') - 6 first_day
??from (select substr('2004-33', 1, 4) yy, to_number(substr('2004-33', 6)) ww
? ?? ?? ? from dual)
? ?? ?? ?
select trunc(to_date(substr('2003-01',1,5)||to_char((to_number(substr('2003-01',6)))*7),'yyyy-ddd'),'d') last_day from dual

select max(v_date) from
??(select (to_date('200408','yyyymm') + rownum) v_date
??from all_tables
??where rownum < 370)
where to_char(v_date,'yyyy-iw') = '2004-33'

3.查詢某周的日期
select min_date, to_char(min_date,'day') day from
(select to_date(substr('2004-33',1,4)||'001'+rownum-1,'yyyyddd') min_date
? ?? ???from all_tables
??where rownum <= decode(mod(to_number(substr('2004-33',1,4)),4),0,366,365)??
??union

??select to_date(substr('2004-33',1,4)-1||
? ?? ?? ?decode(mod(to_number(substr('2004-33',1,4))-1,4),0,359,358)+rownum,'yyyyddd') min_date
? ?? ???from all_tables? ?? ?? ?
? ?? ?? ? where rownum <= 7
??union

??select to_date(substr('2004-33',1,4)+1||'001'+rownum-1,'yyyyddd') min_date
? ?? ???from all_tables? ?? ?? ?
? ?? ?? ? where rownum <= 7? ?? ?? ?? ?? ?? ?? ???
)
where to_char(min_date,'yyyy-iw') ='2004-33'

?

oracle中時間運算

論壇中常常看到有對oracle中時間運算提問的問題,今天有時間,看了看以前各位兄弟的貼子,整理了一下,并作了個示例,希望會對大家有幫助。
首先感謝ern、eric.li及各版主還有熱心的兄弟們

內容如下:
1、oracle支持對日期進行運算
2、日期運算時是以天為單位進行的
3、當需要以分秒等更小的單位算值時,按時間進制進行轉換即可
4、進行時間進制轉換時注意加括號(見示例中紅色括號),否則會出問題

SQL> alter session set nls_date_format='yyyy-mm-dd hh:mi:ss';

會話已更改。

SQL> set serverout on
SQL> declare
??2 DateValue date;
??3 begin
??4 select sysdate into DateValue from dual;
??5 dbms_output.put_line('源時間:'||to_char(DateValue));
??6 dbms_output.put_line('源時間減1天:'||to_char(DateValue-1));
??7 dbms_output.put_line('源時間減1天1小時:'||to_char(DateValue-1-1/24));
??8 dbms_output.put_line('源時間減1天1小時1分:'||to_char(DateValue-1-1/24-1/(24*60)));
??9 dbms_output.put_line('源時間減1天1小時1分1秒:'||to_char(DateValue-1-1/24-1/(24*60)-1/(24*60*6
0)));
10 end;
11 /
源時間:2003-12-29 11:53:41
源時間減1天:2003-12-28 11:53:41
源時間減1天1小時:2003-12-28 10:53:41
源時間減1天1小時1分:2003-12-28 10:52:41
源時間減1天1小時1分1秒:2003-12-28 10:52:40

PL/SQL 過程已成功完成。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/387904.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/387904.shtml
英文地址,請注明出處:http://en.pswp.cn/news/387904.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

BZOJ4868 Shoi2017期末考試(三分+貪心)

容易想到枚舉最晚發布成績的課哪天發布&#xff0c;這樣與ti和C有關的貢獻固定。每門課要么貢獻一些調節次數&#xff0c;要么需要一些調節次數&#xff0c;剩下的算貢獻也非常顯然。這樣就能做到平方級別了。 然后大膽猜想這是一個凸函數三分就能A掉了。具體的&#xff0c;延遲…

SQL的執行計劃

SQL的執行計劃實際代表了目標SQL在Oracle數據庫內部的具體執行步驟&#xff0c;作為調優&#xff0c;只有知道了優化器選擇的執行計劃是否為當前情形下最優的執行計劃&#xff0c;才能夠知道下一步往什么方向。 執行計劃的定義&#xff1a;執行目標SQL的所有步驟的組合。 我們首…

問卷 假設檢驗 t檢驗_真實問題的假設檢驗

問卷 假設檢驗 t檢驗A statistical Hypothesis is a belief made about a population parameter. This belief may or might not be right. In other words, hypothesis testing is a proper technique utilized by scientist to support or reject statistical hypotheses. Th…

webpack打包ES6降級ES5

Babel是一個廣泛使用的轉碼器&#xff0c;babel可以將ES6代碼完美地轉換為ES5代碼&#xff0c;所以我們不用等到瀏覽器的支持就可以在項目中使用ES6的特性。 安裝babel實現ES6到ES5 npm install -D babel-core babel-preset-es2015 復制代碼安裝babel-loader npm install -D ba…

[轉帖]USB-C和Thunderbolt 3連接線你搞懂了嗎?---沒搞明白.

USB-C和Thunderbolt 3連接線你搞懂了嗎&#xff1f; 2018年11月25日 07:30 6318 次閱讀 稿源&#xff1a;威鋒網 3 條評論按照計算行業的風潮&#xff0c;USB Type-C 將會是下一代主流的接口。不過&#xff0c;在過去兩年時間里&#xff0c;關于 USB-C、Thunderbolt 3、USB 3.1…

sqldeveloper的查看執行計劃快捷鍵F10

簡介&#xff1a;本文全面詳細介紹oracle執行計劃的相關的概念&#xff0c;訪問數據的存取方法&#xff0c;表之間的連接等內容。并有總結和概述&#xff0c;便于理解與記憶!目錄---一&#xff0e;相關的概念Rowid的概念Recursive Sql概念Predicate(謂詞)DRiving Table(驅動表)…

大數據技術 學習之旅_為什么聚焦是您數據科學之旅的關鍵

大數據技術 學習之旅David Robinson, a data scientist, has said the following quotes:數據科學家David Robinson曾說過以下話&#xff1a; “When you’ve written the same code 3 times, write a function.”“當您編寫了3次相同的代碼時&#xff0c;請編寫一個函數。” …

SQL 語句

去重字段里的值 SELECT DISTINCT cat_id,goods_sn,repay FROM ecs_goods where cat_id ! 20014 刪除除去 去重字段 DELETE FROM ecs_goods where goods_id NOT IN ( select bid from (select min(goods_id) as bid from ecs_goods group by cat_id,goods_sn,repay) as b );轉…

無監督學習 k-means_無監督學習-第4部分

無監督學習 k-means有關深層學習的FAU講義 (FAU LECTURE NOTES ON DEEP LEARNING) These are the lecture notes for FAU’s YouTube Lecture “Deep Learning”. This is a full transcript of the lecture video & matching slides. We hope, you enjoy this as much as …

vCenter 升級錯誤 VCSServiceManager 1603

近日&#xff0c;看到了VMware發布的vCenter 6.7 Update 1b的更新消息。其中有一條比較震撼。有誤刪所有VM的概率&#xff0c;這種BUG誰也承受不起。Removing a virtual machine folder from the inventory by using the vSphere Client might delete all virtual machinesIn t…

day28 socketserver

1. socketserver 多線程用的 例 import socket import timeclientsocket.socket() client.connect(("127.0.0.1",9000))while 1:cmdinput("請輸入指令")client.send(cmd.encode("utf-8"))from_server_msgclient.recv(1024).decode("utf…

車牌識別思路

本文源自我之前花了2天時間做的一個簡單的車牌識別系統。那個項目&#xff0c;時間太緊&#xff0c;樣本也有限&#xff0c;達不到對方要求的95%識別率&#xff08;主要對于車牌來說&#xff0c;D,0&#xff0c;O&#xff0c;I&#xff0c;1等等太相似了。然后&#xff0c;漢字…

深度學習算法原理_用于對象檢測的深度學習算法的基本原理

深度學習算法原理You just got a new drone and you want it to be super smart! Maybe it should detect whether workers are properly wearing their helmets or how big the cracks on a factory rooftop are.您剛剛擁有一架新無人機&#xff0c;并希望它變得超級聰明&…

【python】numpy庫linspace相同間隔采樣 詳解

linspace可以用來實現相同間隔的采樣&#xff1b; numpy.linspace(start,stop,num50,endpointTrue,retstepFalse, dtypeNone) 返回num均勻分布的樣本&#xff0c;在[start, stop]。 Parameters(參數): start : scalar(標量) The starting value of the sequence(序列的起始點)…

Spring整合JMS——基于ActiveMQ實現(一)

Spring整合JMS——基于ActiveMQ實現&#xff08;一&#xff09; 1.1 JMS簡介 JMS的全稱是Java Message Service&#xff0c;即Java消息服務。它主要用于在生產者和消費者之間進行消息傳遞&#xff0c;生產者負責產生消息&#xff0c;而消費者負責接收消息。把它應用到實際的…

軟件本地化 pdf_軟件本地化與標準翻譯

軟件本地化 pdfSoftware has become such an essential part of our world that it’s impossible to imagine a life without it. There’s hardly a service or product around us that wasn’t created with software or that runs on software.軟件已成為我們世界的重要組成…

CentOS7+CDH5.14.0安裝全流程記錄,圖文詳解全程實測-8CDH5安裝和集群配置

Cloudera Manager Server和Agent都啟動以后&#xff0c;就可以進行CDH5的安裝配置了。 準備文件 從 http://archive.cloudera.com/cdh5/parcels/中下載CDH5.14.0的相關文件 把CDH5需要的安裝文件放到主節點上&#xff0c;新建目錄為/opt/cloudera/parcel-repo把我們之前下載的…

node.js安裝部署測試

&#xff08;一&#xff09;安裝配置&#xff1a; 1&#xff1a;從nodejs.org下載需要的版本 2&#xff1a;直接安裝&#xff0c;默認設置 &#xff0c;默認安裝在c:\program files\nodejs下。 3&#xff1a;更改npm安裝模塊的默認目錄 &#xff08;默認目錄在安裝目錄下的node…

數據庫不停機導數據方案_如何計算數據停機成本

數據庫不停機導數據方案In addition to wasted time and sleepless nights, data quality issues lead to compliance risks, lost revenue to the tune of several million dollars per year, and erosion of trust — but what does bad data really cost your company? I’…

luogu4159 迷路 (矩陣加速)

考慮如果只有距離為1的邊&#xff0c;那我用在時間i到達某個點的狀態數矩陣 乘上轉移矩陣&#xff08;就是邊的鄰接矩陣&#xff09;&#xff0c;就能得到i1時間的 然后又考慮到邊權只有1~9&#xff0c;那可以把邊拆成只有距離為1的 具體做法是一個點拆成9個然后串聯 1 #includ…