Connecting Tech Pros Worldwide Forums | Help | Site Map

Important Date Related Queries - 1

debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,511
#1   Sep 6 '07
This article consistes of some of the frequently asked date related queries.
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
  1. Select next_day(trunc(sysdate,'mm')-1,'SUNDAY') First_Sunday,    next_day(last_day(trunc(sysdate,'mm'))-7,'SUNDAY') Last_Sunday from dual
For other days please modify the name of the DAY in the query.

2.Determining the First and Last Day of a Month
------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. select trunc(sysdate,'mm') Firstday,
  2. last_day(sysdate) Lastday  from dual
3.Extracting Units of Time from a Date
------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. Select to_number(to_char(sysdate,'hh24')) hour,
  2. to_number(to_char(sysdate,'mi')) min,
  3. to_number(to_char(sysdate,'ss')) sec,
  4. to_number(to_char(sysdate,'dd')) day,
  5. to_number(to_char(sysdate,'mm')) mth,
  6. to_number(to_char(sysdate,'yyyy')) year
  7. from dual
4.To find out No of days in year.
---------------------------------
Expand|Select|Wrap|Line Numbers
  1. select add_months(trunc(sysdate,'y'),12) - trunc(sysdate,'y') from dual
5.To find out if the year is a leap Year or not.
---------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. select decode(to_char(last_day(add_months(trunc(sysdate,'y'),1)),'DD'),28 ,'Not a Leap Year','Leap Year')   from dual
6.Listing Quarter Start and End Dates for the Year
-------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. Select rownum qtr,
  2. 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.Retrivinging three most recent dates from a table
=================================
Expand|Select|Wrap|Line Numbers
  1. select e.hiredate from
  2. (
  3. select dense_rank() over (order by hiredate desc)n,hiredate
  4. from emp )e where e.n<=3
8.Determining the Number of Months or Years Between Two Dates
---------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. select months_between(max_hd,min_hd) MONTHS,
  2. months_between(max_hd,min_hd)/12 YEARS
  3. from (select min(hiredate) min_hd, max(hiredate) max_hd from emp ) x
9.Determining the Number of Seconds, Minutes, or Hours Between Two Dates
------------------------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. 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) -
  2. max(case when ename = 'ALLEN'                  then hiredate end)) as dy  from emp) x
10.Determining the Date Difference Between the Current Record and the Next Record
-----------------------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. select ename, hiredate, next_hd,
  2. next_hd - hiredate diff from (
  3. select deptno, ename, hiredate, lead(hiredate)over(order by hiredate) next_hd
  4. from emp)     ORDER BY HIREDATE
11.Counting the Occurrences of Weekdays in a Year
--------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. with x as ( select level lvl from dual connect by level <= (         add_months(trunc(sysdate,'y'),12)-trunc(sysdate,'y')) 
  2. )  select to_char(trunc(sysdate,'y')+lvl-1,'DAY'), count(*) from x  group by to_char(trunc(sysdate,'y')+lvl-1,'DAY')
Also check Important Date Related Queries - 2



Reply


Similar Oracle Database bytes