国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > Oracle > 正文

Oracle查詢優(yōu)化日期運(yùn)算實(shí)例詳解

2024-08-29 14:00:08
字體:
供稿:網(wǎng)友

 加減日、月、年

在Oracle中,date類型可以直接加減天數(shù),而加減月份要用add_months函數(shù).

select a.hiredate 雇用日期,    a.hiredate + 5 加5天,    a.hiredate - 5 減5天,    add_months(hiredate, 5) 加5個(gè)月,    add_months(hiredate, -5) 減5個(gè)月,    add_months(hiredate, 5 * 12) 加5年,    add_months(hiredate, -5 * 12) 減5年 from emp a where rownum <=1;雇用日期 加5天 減5天 加5個(gè)月 減5個(gè)月 加5年 減5年----------- ----------- ----------- ----------- ----------- ----------- -----------1980-12-17 1980-12-22 1980-12-12 1981-05-17 1980-07-17 1985-12-17 1975-12-17

加減時(shí)、分、秒

上面講過,date可以直接加減天數(shù),那么1/24就是一小時(shí),分鐘與秒的加減類同。

select a.hiredate,    a.hiredate - 5 / 24 / 60 / 60 減5秒,    a.hiredate + 5 / 24 / 60 / 60 加5秒,    a.hiredate - 5 / 24 / 60 減5分鐘,    a.hiredate + 5 / 24 / 60 加5分鐘,    a.hiredate - 5 / 24 減5小時(shí),    a.hiredate + 5 / 24 加5小時(shí) from emp a where rownum <= 1;

如果執(zhí)行時(shí),報(bào)錯(cuò)

Oracle查詢優(yōu)化日期運(yùn)算,Oracle,日期運(yùn)算

數(shù)據(jù)庫服務(wù)器端的編碼和客戶端字符集編碼不一致。

我這邊用的數(shù)據(jù)庫西班牙語,不支持中文。

日期間隔之時(shí)、分、秒

SQL> select duration,    duration * 24 間隔小時(shí),    duration * 24 * 60 間隔分鐘,    duration * 24 * 60 * 60 間隔秒 from (select (max(a.hiredate) - min(a.hiredate)) as duration from emp a where    a.ename in ('ALLEN', 'WARD')) x; DURATION    間隔小時(shí)    間隔分鐘    間隔秒---------- ---------- ---------- ----------     2     48    2880   172800

日期間隔之日、月、年

加減月份用函數(shù) add_months, 而計(jì)算月份間隔就要用函數(shù)months_between

select max_hd - min_hd 間隔天,    months_between(max_hd, min_hd) 間隔月,    months_between(max_hd, min_hd) /12 間隔年 from (select min(hiredate) min_hd, max(hiredate) max_hd from emp) x;    間隔天    間隔月    間隔年---------- ---------- ----------   2348 77.1935483 6.43279569

計(jì)算一年中周內(nèi)各日期的次數(shù)

問題

計(jì)算一年中周內(nèi)各日期(星期日、星期一 ……星期六)的次數(shù)。

解決方案

要計(jì)算一年中周內(nèi)各日期分別有多少個(gè),必須:

  1. 生成一年內(nèi)的所有日期。
  2. 設(shè)置日期格式,得到每個(gè)日期對(duì)應(yīng)為星期幾。
  3. 計(jì)數(shù)周內(nèi)各日期分別有多少個(gè)。
with x as (select level lvl  from dual connect by level <=       (add_months(trunc(sysdate, 'y'), 12) - trunc(sysdate, 'y')))select to_char(trunc(sysdate, 'y') + lvl - 1, 'DAY'), count(*) from x group by to_char(trunc(sysdate, 'y') + lvl - 1, 'DAY');

Oracle查詢優(yōu)化日期運(yùn)算,Oracle,日期運(yùn)算

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到oracle教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 清涧县| 清苑县| 化州市| 八宿县| 旌德县| 会昌县| 蓬莱市| 彭州市| 玉田县| 三亚市| 尖扎县| 格尔木市| 东城区| 寿阳县| 临沭县| 内乡县| 新乡县| 金溪县| 马鞍山市| 临泽县| 佛山市| 昆山市| 特克斯县| 宽甸| 榆中县| 齐齐哈尔市| 垣曲县| 双鸭山市| 昆明市| 阳谷县| 远安县| 太原市| 南京市| 合川市| 民权县| 玉树县| 乌苏市| 汉寿县| 双柏县| 高碑店市| 多伦县|