本文包含一些與日期相關的常見查詢。
希望使用者發現它有用。
1.確定乙個月中特定工作日的第一次和最後一次發生的日期
select next_day(trunc(sysdate,'mm')-1,'sunday') first_sunday, next_day(last_day(trunc(sysdate,'mm'))-7,'sunday') last_sunday from dual
對於其他日子,請在查詢中修改day的名稱。
2.確定乙個月的第一天和最後一天
select trunc(sysdate,'mm') firstday,
last_day(sysdate) lastday from dual
3從日期中提取時間單位select to_number(to_char(sysdate,'hh24')) hour,
to_number(to_char(sysdate,'mi')) min,
to_number(to_char(sysdate,'ss')) sec,
to_number(to_char(sysdate,'dd')) day,
to_number(to_char(sysdate,'mm')) mth,
to_number(to_char(sysdate,'yyyy')) year
from dual
4.找出一年中的天數。select add_months(trunc(sysdate,'y'),12) - trunc(sysdate,'y') from dual
5.找出年份是否為a年。select decode(to_char(last_day(add_months(trunc(sysdate,'y'),1)),'dd'),28 ,'not a leap year','leap year') from dual
6.列出年度的季度開始和結束日期select rownum qtr,
add_months(trunc(sysdate,'y'),(rownum-1)*3) q_start, add_months(trunc(sysdate,'y'),rownum*3)-1 q_end from emp where rownum <= 4
7,從表中檢索三個最新日期select e.hiredate from
(select dense_rank() over (order by hiredate desc)n,hiredate
from emp )e where e.n<=3
8.確定兩個日期之間的月數或年數select months_between(max_hd,min_hd) months,
months_between(max_hd,min_hd)/12 years
from (select min(hiredate) min_hd, max(hiredate) max_hd from emp ) x
9,確定兩個日期之間的秒數,分鐘數或小時數select dy*24 as hr, dy*24*60 as min, dy*24*60*60 as sec from (select (max(case when ename = 'ward' then hiredate end) -
max(case when ename = 'allen' then hiredate end)) as dy from emp) x
10.確定當前記錄和下一條記錄之間的日期差select ename, hiredate, next_hd,
next_hd - hiredate diff from (
select deptno, ename, hiredate, lead(hiredate)over(order by hiredate) next_hd
from emp) order by hiredate
11,計算一年中平日的發生次數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')
還要檢查重要日期相關查詢-2
from:
MySQL 日期相關查詢
select from 表名 where to days 時間欄位名 to days now select from 表名 where to days now to days 時間欄位名 1 select from 表名 where date sub curdate interval 7 day d...
MySQL日期相關查詢語句
日期字串轉時間戳,10位,資料庫儲存的是13位,記得除以1000 select unix timestamp 2019 09 01 00 00 00 時間戳轉日期 select from unixtime 1567267200,y m d h i s 查詢當天,格式為yyyy mm dd hh mm...
mysql日期查詢 mysql 查詢日期
檢視本月資料 select from content publish where date format publish time,y m date format date sub curdate interval 0 month y m 檢視上個月資料 select from content pu...