how to get previous 11 month names from sysdate using oracle - sql

how to get previous 11 month names from sysdate using oracle
i tried like , iam getting month names between some dates
select add_months (trunc (to_date('10/18/2007','MM/DD/YYYY'), 'MM'), 1*Level -1)
Month FROM Dual
CONNECT BY Level <= MONTHS_BETWEEN(to_date('11/17/2008','MM/DD/YYYY'), to_date('10/18/2007','MM/DD/YYYY')) + 1
order by month

May be you need something like that:
select to_char(add_months (to_date('10/18/2007','MM/DD/YYYY'), -1* Level), 'Mon-YYYY')
Month
FROM Dual
CONNECT BY Level <= 11
order by month

YOU CAN USE THIS:
SELECT TO_CHAR( ADD_MONTHS(SYSDATE,-1*LEVEL ),'MON') MONTH_NAME
FROM DUAL
CONNECT BY LEVEL <= MONTHS_BETWEEN( TRUNC(SYSDATE,'MM') , ADD_MONTHS( TRUNC(SYSDATE,'MM'),-11))
ORDER BY LEVEL

Related

Get whole month dates based on day name

I want to get all dates of a month based on day name like if I want to get all FRIDAY dates from current date to a specific end date. I got a solution in SQL but can't find any solution in ORACLE.
You can use hierarchy query with next_day function as follows:
-- input variable
with dataa (start_date, end_date) as
(select date '2020-11-01', date '2020-12-15' from dual)
-- your query starts from here
select next_day((level-1)*7 + (start_date - 1), 'FRIDAY')
FROM DATAA
connect by
next_day((level-1)*7 + (start_date - 1),'FRIDAY') <= end_date
Db<>fiddle here
You can use hierarchical query as
WITH t AS
(
SELECT TRUNC(sysdate,'MM')+level-1 AS Fridays
FROM dual
WHERE TO_CHAR(TRUNC(sysdate,'MM')+level-1,'Dy','NLS_DATE_LANGUAGE=American')='Fri'
CONNECT BY level <= LAST_DAY(TRUNC(sysdate)) - TRUNC(sysdate,'MM') + 1
)
SELECT *
FROM t
WHERE Fridays BETWEEN :data1 AND :date2
Demo
Using connect by then filtering by Friday is a simple solution. In the example below I am using July 1st as the start date and December 1st as the end date.
SELECT DATE '2020-7-1' + LEVEL AS friday_dates
FROM DUAL
WHERE TO_CHAR (DATE '2020-7-1' + LEVEL, 'DY') = 'FRI'
CONNECT BY LEVEL <= DATE '2020-12-1' - DATE '2020-7-1';

Get the last day of each month in the last two years using oracle SQL

I am new to Oracle SQL. I know the LAST_DAY function will return the last day of the month given a specific date. But how can I get the last day of each month in the last two years? Thanks so much!
SELECT TRUNC (ADD_MONTHS (SYSDATE, -(LEVEL - 1)), 'MM') FIRST_DAY,
LAST_DAY (ADD_MONTHS (SYSDATE, -(LEVEL - 1))) LAST_DAY
FROM DUAL
CONNECT BY LEVEL <= 24;
Here you go:
WITH cteTwo_years_ago AS (SELECT TRUNC(SYSDATE - INTERVAL '2' YEAR, 'MONTH') AS TWO_YEARS_AGO
FROM DUAL),
cteMonth_intervals AS (SELECT INTERVAL '1' MONTH * (LEVEL-1) AS SEQ
FROM DUAL
CONNECT BY LEVEL-1 < 24),
cteDates AS (SELECT TWO_YEARS_AGO + SEQ AS MONTH_START
FROM cteTwo_years_ago
CROSS JOIN cteMonth_intervals)
SELECT LAST_DAY(MONTH_START)
FROM cteDates
First and last date of each month in interval from given date up today.
DEFINE start_date=TO_DATE('01.01.2020','DD.MM.YYYY');
SELECT
ADD_MONTHS(&start_date,LEVEL-1) first_day_in_month,
ADD_MONTHS(&start_date,LEVEL)-1 last_day_in_month
FROM DUAL CONNECT BY LEVEL <= FLOOR(MONTHS_BETWEEN(SYSDATE,&start_date));

Need to return sysdate and past 9 dates

With the code below I can return this month and past 6 months.
My code:
SELECT TO_CHAR(add_months(TRUNC(to_date( sysdate),'Month'), -rownum+1), 'Month') mon,
rownum month_order
FROM dual
CONNECT BY rownum <=
(SELECT COUNT(mon)
FROM
(SELECT TO_CHAR( add_months( start_date, level-1 ), 'fmMonth' ) AS mon
FROM
(SELECT to_date( add_months(TRUNC(sysdate),-6)) start_date,
to_date( sysdate) end_date
FROM dual)
CONNECT BY level <= months_between( TRUNC(end_date,'MM'), TRUNC(start_date,'MM') ) + 1) dual);
In the same way I have to return this date and past 9 dates.
Please help me on this to return 10 dates by using connect by.
Thanks in advance.
Here's a query for the last ten days.
select sysdate - (level-1)
, level as day_order
from dual
connect by level <= 10;
Your month query seems extremely over-engineered. This would do the same thing:
select to_char(add_months(trunc(sysdate, 'MM'), 1 - level), 'Month')
, level as month_order
from dual
connect by level <= 7;
"Let suppose if the data is available on today's date and remaining 9 date's doesn't have any data but it has to display the count as zero"
Use the generated result set in an outer join:
with q as (
select sysdate - (level-1) as dt
, level as day_order
from dual
connect by level <= 10
)
select q.dt as txn_date
, sum(t42.col1)
from q
left outer join t42
on t42.transaction_date = q.dt
group by q.dt;
Try this -
SELECT ( TRUNC( SYSDATE ) + 1 ) - ROWNUM
FROM DUAL
CONNECT BY ROWNUM <= 10
In the same way i have to return this date and past 9 dates.
This will get today and the past 9 days:
SELECT TRUNC( SYSDATE ) - LEVEL + 1 AS day
FROM DUAL
CONNECT BY LEVEL <= 10
this month and past 6 months
SELECT TO_CHAR( ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), 1 - LEVEL ), 'Month' ) AS month
FROM DUAL
CONNECT BY LEVEL <= 7

How do I get all dates from sysdate's month with Oracle SQL?

I try to get a query without parameters to obtain the list of dates from the current month.
Something like this:
SYSDATE = 16/07/15
I want the next list:
01/07/15
02/07/15
...
30/07/15
31/07/15
Here's what I got to work:
SELECT TRUNC(SYSDATE, 'MM') + LEVEL - 1 AS day
FROM dual
CONNECT BY TRUNC(TRUNC(SYSDATE, 'MM') + LEVEL - 1, 'MM') = TRUNC(SYSDATE, 'MM')
;
The key in this query is TRUNC(SYSDATE, 'MONTH') which is the first day of the current month.
We use hierarchical queries to keep adding one day to the first day of the month until the value is no longer in the current month.
We use LEVEL - 1 because LEVEL starts from 1 and we need it to start from zero.
Here's a pseudo-query for what the above query does:
SELECT (start_of_month + days) AS day
FROM dual
WHILE MONTH_OF(start_of_month + days) = current_month
This query be a bit easier to understand:
SELECT *
FROM
(
SELECT TRUNC(SYSDATE, 'MM') + LEVEL - 1 AS day
FROM dual
CONNECT BY LEVEL <= 32
)
WHERE EXTRACT(MONTH FROM day) = EXTRACT(MONTH FROM SYSDATE)
Selects all the days for current month
SELECT TO_CHAR (TRUNC (SYSDATE, 'MM'), 'YYYYMMDD')+(LEVEL - 1) each_date
FROM DUAL a
CONNECT BY LEVEL < (TO_NUMBER (TO_CHAR (TRUNC (SYSDATE, 'MM') - 1, 'DD'))+1)
This should work:
select trunc(sysdate, 'MONTH') + rownum - 1
from dual
connect by rownum <= to_number(to_char(last_day(sysdate), 'DD'));
This is a trick using connect by. I truncate the date at the month level, effectively setting it to the 1st day of the month, add a day for each "level" and then filter for any day that occurs outside the month.
select day_of_month
from
(select (level - 1) + trunc(to_date('07/16/2015','MM/DD/YYYY'),'MM') day_of_month
from
dual
connect by level <= 31)
where day_of_month < add_months(trunc(to_date('07/16/2015','MM/DD/YYYY'),'MM'),1);

Oracle SQL first and last day of quarter of any year

Is there any way i can calculate the first and last day of the three quarters in any year . 2012 , 2013 or 2014
SELECT ADD_MONTHS(TRUNC(SYSDATE, 'Q'), -3) AS First,
TRUNC(SYSDATE, 'Q') - 1 AS Last
FROM DUAL
calculates the first quarter of current year. i want to calculate the first quarter of any year ?
You could do the following:
with q(qtr) as(
select add_months(
DATE '2013-01-01'
, (level-1)*3
)
from dual
connect by level <= 4
)
select qtr as first_day
, last_day(add_months(qtr, 2)) as last_day
from q
Result:
FIRST_DAY LAST_DAY
----------- -----------
01.01.2013 31.03.2013
01.04.2013 30.06.2013
01.07.2013 30.09.2013
01.10.2013 31.12.2013
SQLFIddle Demo
This is one way of doing it
select to_date('01-JAN-'||to_char(yr), 'DD-MON-YYYY') first_qtr,
to_date('01-APR-'||to_char(yr), 'DD-MON-YYYY') second_qtr,
to_date('01-JUL-'||to_char(yr), 'DD-MON-YYYY') third_qtr,
to_date('01-OCT-'||to_char(yr), 'DD-MON-YYYY') fourth_qtr
from ( select :year yr from dual )
UNION ALL
select to_date('01-APR-'||to_char(yr), 'DD-MON-YYYY')-1 first_qtr,
to_date('01-JUL-'||to_char(yr), 'DD-MON-YYYY')-1 second_qtr,
to_date('01-OCT-'||to_char(yr), 'DD-MON-YYYY')-1 third_qtr,
to_date('01-JAN-'||to_char(yr+1), 'DD-MON-YYYY')-1 fourth_qtr
from ( select :year yr from dual )
I have used bind variables so change it to your requirements accordingly.
I am fairly new to Oracle, so other's can give a simplified code.
The output when given 2009 would be as below
FIRST_QTR SECOND_QTR THIRD_QTR FOURTH_QTR
01/01/2009 04/01/2009 07/01/2009 10/01/2009
03/31/2009 06/30/2009 09/30/2009 12/31/2009
This is an old question but maybe this will be helpful:
WITH y1 AS (
SELECT LEVEL + 2000 AS the_year
FROM dual
CONNECT BY LEVEL <= 20
), q1 AS (
SELECT LEVEL AS the_quarter
FROM dual
CONNECT BY LEVEL <= 4
)
SELECT the_year, the_quarter
, TO_CHAR(first_day, 'DAY') AS first_dw, first_day
, TO_CHAR(last_day, 'DAY') AS last_dw, last_day
FROM (
SELECT the_year, the_quarter
, ADD_MONTHS(TO_DATE(the_year, 'YYYY'), 3 * (the_quarter - 1)) AS first_day
, ADD_MONTHS(TO_DATE(the_year, 'YYYY'), 3 * the_quarter) - 1 AS last_day
FROM y1, q1
)
One line per year, each line consisting of the year plus 8 (=2 dates per quarter) dates:
with params as (
select
2012 as start_year,
2014 as end_year
from
dual
)
select
start_year+ level - 1 year,
to_date((start_year+ level - 1) || '0101', 'yyyymmdd') start_q1,
to_date((start_year+ level - 1) || '0331', 'yyyymmdd') end_q1 ,
to_date((start_year+ level - 1) || '0401', 'yyyymmdd') start_q2,
to_date((start_year+ level - 1) || '0630', 'yyyymmdd') end_q2 ,
to_date((start_year+ level - 1) || '0701', 'yyyymmdd') start_q3,
to_date((start_year+ level - 1) || '0930', 'yyyymmdd') end_q3 ,
to_date((start_year+ level - 1) || '1001', 'yyyymmdd') start_q4,
to_date((start_year+ level - 1) || '1231', 'yyyymmdd') end_q4
from
dual, params
connect by
start_year + level -1 <= end_year;