Hope the users find it useful.
==========================
1.Determining the Date of the First and Last Occurrence of a Specific Weekday in a Month
------------------------------------
Expand|Select|Wrap|Line Numbers
- Select next_day(trunc(sysdate,'mm')-1,'SUNDAY') First_Sunday, next_day(last_day(trunc(sysdate,'mm'))-7,'SUNDAY') Last_Sunday from dual
2.Determining the First and Last Day of a Month
------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
- select trunc(sysdate,'mm') Firstday,
- last_day(sysdate) Lastday from dual
------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
- 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
---------------------------------
Expand|Select|Wrap|Line Numbers
- select add_months(trunc(sysdate,'y'),12) - trunc(sysdate,'y') from dual
---------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
- select decode(to_char(last_day(add_months(trunc(sysdate,'y'),1)),'DD'),28 ,'Not a Leap Year','Leap Year') from dual
-------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
- 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
=================================
Expand|Select|Wrap|Line Numbers
- select e.hiredate from
- (
- select dense_rank() over (order by hiredate desc)n,hiredate
- from emp )e where e.n<=3
---------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
- 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
------------------------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
- 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
-----------------------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
- 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
--------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
- 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')