Question: How do I write a SQL to display
the number of months between two dates?
Answer:
Here is a simple way to display all months between a range of dates,
without referencing any table:
SELECT
MOD( TRUNC( MONTHS_BETWEEN( '2015-07-29', '2015-03-28' ) ), 12 ) as
MONTHS
FROM
DUAL;
Here is a way to name all of the months between two dates:
select
to_char(which_month, 'Mon-yyyy') month
from
(
select
add_months(to_date('01-2016','mm-yyyy'),
rownum-1) which_month
from
dba_objects
where
rownum <= months_between(to_date('12-2016','mm-yyyy'),
add_months(to_date('01-2016','mm-yyyy'), -1))
order by
which_month
);
Here is a script that uses the last_day function to show
the number of months between two dates:
select
distinct(last_day(to_date(td.end_date + 1 - rownum)))
from
all_objects,
(-- this is just to easily substitute dates
for the example...
select to_date('30-JAN-2010') start_date
,to_date('15-MAR-2011') end_date
FROM DUAL ) td
where
trunc(td.end_date + 1 -
rownum,'MM') >= trunc(td.start_date,'MM')
order by 1
(LAST_DAY
---------
31-JAN-10
28-FEB-10
31-MAR-10
30-APR-10
31-MAY-10
30-JUN-10
31-JUL-10
|
|
Get the Complete
Oracle SQL Tuning Information
The landmark book
"Advanced Oracle
SQL Tuning The Definitive Reference" is
filled with valuable information on Oracle SQL Tuning.
This book includes scripts and tools to hypercharge Oracle 11g
performance and you can
buy it
for 30% off directly from the publisher.
|
|
|
Burleson is the American Team
Note:
This Oracle
documentation was created as a support and Oracle training reference for use by our
DBA performance tuning consulting professionals.
Feel free to ask questions on our
Oracle forum.
Verify
experience!
Anyone
considering using the services of an Oracle support expert should
independently investigate their credentials and experience, and not rely on
advertisements and self-proclaimed expertise. All legitimate Oracle experts
publish
their Oracle
qualifications.
Errata?
Oracle technology is changing and we
strive to update our BC Oracle support information. If you find an error
or have a suggestion for improving our content, we would appreciate your
feedback. Just
e-mail:
and include the URL for the page.
Copyright © 1996 - 2020
All rights reserved by
Burleson
Oracle ®
is the registered trademark of Oracle Corporation.
|
|