473,385 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

Important Date Related Queries - 1

debasisdas
8,127 Expert 4TB
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
Sep 6 '07 #1
0 5716

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: DCM Fan | last post by:
{CREATE TABLEs and INSERTs follow...} Gents, I have a main table that is in ONE-MANY with many other tables. For example, if the main table is named A, there are these realtionships: A-->B...
9
by: Thomas R. Hummel | last post by:
Hello, I am importing data that lists rates for particular coverages for a particular period of time. Unfortunately, the data source isn't very clean. I've come up with some rules that I think...
6
by: BlackFireNova | last post by:
Using Access 2002 I am writing a report which draws data from several different tables. I can't link all the tables in a query, as some can not be related without truncating the data. I plan...
5
by: David B | last post by:
I have a number of queries, running one after the other, which do quite a complex calculation. A text box on a form provides the date for this routine. I have another routine I wish to do and it...
2
by: carl.barrett | last post by:
Hi, I'm back with the same question as I still can't get it to display my data the way I want it to. The table lists information about a perpetrator involved with an anti social behaviour...
6
by: Tony Miller | last post by:
All I have an aggregate query using the function Month & Year on a datereceived field ie: TheYear: Year() TheMonth: Month() These are the group by fields to give me a Count on another field by...
67
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 ...
9
by: mharrison | last post by:
Hello, I am developing a small java web-based car-pool booking system app which interacts with an access database. I am trying to write 2 queries: The first which will specify whether a given car...
0
debasisdas
by: debasisdas | last post by:
12.Alternate method for determining Quarter Start and End Dates for Quarter(not recommended) ---------------------------------------------------------------------------------------------- select...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.