Calculating date range for Oracle - oracle

I'm automating an Oracle script, which needs to select data from the first day of last month, to the first day of this month (this report is run on the second day of every month). But, no matter what combination of LAST_DAY, TRUNC, TO_CHAR, TO_DATE, ADD_MONTHS, etc have worked.
Can someone help me figure out how to automate the calculation for:
WHERE date BETWEEN (first day of last month) AND (first day of this month)

You could use TRUNC:
WHERE date between trunc(trunc(sysdate,'MM')-1,'MM') and trunc(sysdate, 'MM');
db<>fiddle demo
select trunc(trunc(sysdate,'MM')-1,'MM') AS "first day of last month",
trunc(sysdate, 'MM') AS "first day of this month"
from dual;
from dual

This did the trick:
BETWEEN TO_CHAR(LAST_DAY(ADD_MONTHS(SYSDATE, -2)) +1, 'YYYY-MM-DD') AND TO_CHAR(LAST_DAY(ADD_MONTHS(SYSDATE, -1)) +1, 'YYYY-MM-DD')

Related

how to get year and month into number oracle

i'm trying to get sysdate year and month into number or char.
I'd want something like this:
202107
or last month
202106
i tried this code:
select trunc(add_months(sysdate, -1), 'MM') from dual;
please help, thanks
Convert it to a string and then to a number:
SELECT TO_NUMBER(TO_CHAR(ADD_MONTHS(SYSDATE,-1), 'YYYYMM'))
FROM DUAL;
Or, you can use extract and wrap it in a sub-query so you do not need to repeat adding the months:
SELECT EXTRACT(YEAR FROM dt) * 100 + EXTRACT(MONTH FROM dt)
FROM (
SELECT ADD_MONTHS(SYSDATE, -1) AS dt
FROM DUAL
)
Like
SELECT EXTRACT(year from sysdate) * 100 + EXTRACT(month from sysdate)
If you want to scroll around, manipulate the date before you extract from it, rather than minusing after you extract (gets tricky to e.g. go back a month if the date is in jan)
--will work for jan 2021
SELECT EXTRACT(year from ADD_MONTHS(somedate, -1)) * 100 + EXTRACT(month from ADD_MONTHS(somedate, -1))
--won't work for jan 2021
SELECT EXTRACT(year from somedate) * 100 + (EXTRACT(month from somedate) - 1)
TRUNC is a device that "rounds" a date to a particular interval, such as "trimming the time off a datetime" or "making any day of the week back to the date that was the start of the week" - very useful for all sorts of stuff like "total sales by month - SUM(sale) GROUP BY TRUNC(saledate, 'mm')" but it keeps all the components of the date (the day, the hour, the minute etc) so it isn't what you want.

Get 9 AM of current day in Oracle

Can anyone tell me how to get 9AM of the current day in Oracle?
In SQL Server I would do the following:
SELECT DATEADD(day, DATEDIFF(day, 0, GETDATE()), '09:00:00')
I'm writing a stored procedure that only retrieves data from 9 AM of the same day.
Just take the current day at midnight and add nine hours
trunc(sysdate) + interval '9' hour
select trunc(sysdate) +9/24 from dual;
Try to get 9AM of the current day in Oracle using the following:
select to_char(trunc(sysdate + 1) + 9/24, 'HH')|| 'AM' as "current day 9AM"
from dual;

How can we achieve last month record in oracle

I am new to oracle and I am writing a query where I have to fetch last month
My query:
select to_char(sysdate,'MON') from dual
But it is giving me
AUG which is current month.
I want the output as JUL which is last month
How can we achieve last month record.
select to_char(ADD_MONTHS (SYSDATE, -1),'MON') from dual
--Move ahead one month:
ADD_MONTHS (SYSDATE, 1);
--Move backward 1 month:
ADD_MONTHS (SYSDATE, -1);
http://www.oracle.com/technetwork/issue-archive/2012/12-jan/o12plsql-1408561.html
Oracle Database offers several built-in functions for shifting a date
by the requested amount or finding a date:
ADD_MONTHS—adds the specified number of months to or subtracts it from a date (or a timestamp)
NEXT_DAY—returns the date of the first weekday named in the call to the function
LAST_DAY—returns the date of the last day of the month of the specified date

How to get data from first day of the month until yesterday

does anyone know the query to get data from first day of the month until yesterday? I Try using query below but the problem is when today is the first day of the month, so the value become between 20170201 and 20170131
select * from a where to_char(DATE,'yyyymmdd') BETWEEN to_char(sysdate,'yyyymm')||'01' and to_char(sysdate-1,'yyyymmdd')
Ex: if today is 01-02-2017 i want to select the data from 01-01-2017 until 31-01-2017, but if today is 13-02-2017 i want to select the data from 01-02-2017 until 12-02-2017, thank's before
This question has been updated with your new logic. The query below will retain records under the following conditions:
someDate falls between the first day and yesterday of the current month, or
someDate, when today is the first of the month, falls anywhere in the previous month
SELECT *
FROM yourTable
WHERE someDate BETWEEN CASE WHEN EXTRACT(DAY FROM SYSDATE) = 1
THEN TRUNC(SYSDATE-1, 'MONTH')
ELSE TRUNC(SYSDATE, 'MONTH') END AND
CASE WHEN EXTRACT(DAY FROM SYSDATE) = 1
THEN TRUNC(SYSDATE, 'MONTH') - 1
ELSE TRUNC(SYSDATE - 1) END
This query assumes that you have a DATE column called someDate.

Between with Variables for prior months start and stop

I use the following WHERE with sysdate to get a between range.
WHERE TO_CHAR(mopstart, 'yyyy-mm-dd hh24:mi:ss') BETWEEN TO_CHAR(sysdate,'YYYY-MM-DD')||' 21:00:00' AND TO_CHAR(sysdate+1,'YYYY-MM-DD')||' 20:59:59'
My question is, how do I create a BETWEEN with variables for the date to return the prior months start and end date?
You would usually not use BETWEEN for this. As in real life you would not say "the time from beginning of last month till the end of last month", but just "last month", so you would do in SQL.
Knowing that a month is actually the year and the month:
where extract(year from mopstart) = extract(year from sysdate)
and extract(month from mopstart) = extract(month from sysdate) - 1
Or:
where to_char(mopstart,'yyyymm') = to_char(add_months(sysdate,-1),'yyyymm')
If I understand you well, that would be:
where ... between trunc(add_months(sysdate, -1), 'MON') and trunc(sysdate, 'MON') - 1
One more thing: don't use TO_CHAR to compare dates, just use dates and intervals.

Resources