Using "today" and "two days before" in date with Oracle - oracle

I have the following WHEREclause in a query:
SELECT ...
FROM ...
WHERE IMPORT_DATE between
to_date('2018-03-16 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and
to_date('2018-03-16 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
And I would like to write two new queries:
One with this same clause but using "today" instead of "2018-03-16".
Another with the same clause but using "the day before yesterday" (today - 2) instead of "2018-03-16".
How can I do this in Oracle?

As Oracle has no real DATE data type and always includes a time, it's usually better to not use between for conditions like that, but to use >= together with < compared to midnight the next day.
To find the rows from "today" use:
SELECT ...
FROM ...
WHERE import_date >= trunc(sysdate)
AND import_date < trunc(sysdate) + 1;
or:
SELECT ...
FROM ...
WHERE import_date >= trunc(sysdate) - 2
AND import_date < (trunc(sysdate) - 2) + 1;
The parentheses aren't really required in the second expression, they are just there to document that it's the same expression as the first one.

select trunc(sysdate) from dual;
Returns today's date without any time
select trunc(sysdate) - interval '2' day from dual;
Returns the day 2 days before today without time.
You can also use month, hour, year etc instead of day.

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.

How to filter particular time period in oracle

I have a date and time column(Login_time) in this format '5/23/2018 3:35:18 PM'. I want to filter rows with August month and time period of 6.30 to 7.30. I am able to filter august month using Login_time >= to_date('08/1/2018','MM/DD/YYYY') and Login_time >= to_date('08/31/2018','MM/DD/YYYY') but now I want rows only between 6.30 am to 7.30 am.
Be careful with date/time ranges. You'd usually want to include the start time and exclude an end time, e.g. all rows from 2018-08-01 until before 2018-09-01, thus including the whole last day, no matter how close to midnight. Here is the whole query:
select *
from mytable
where login_time >= date '2018-08-01'
and login_time < date '2018-09-01'
and to_char(login_time, 'hh24:mi') >= '06:30'
and to_char(login_time, 'hh24:mi') < '07:31';
You could format the time portion of column to a string, and then compare it lexichograpically:
TO_CHAR(login_time, 'HH24:MI:SS') BETWEEN '06:30:00' AND '07:30:00'
You simply include the time:
Login_time >= to_date('08/01/2018 06:30:00','MM/DD/YYYY HH24:MI:SS')
and Login_time <= to_date('08/31/2018 07:30:59','MM/DD/YYYY HH24:MI:SS')
and to_char(login_time, 'hh24:mi:ss') >= '06:30:00'
and to_char(login_time, 'hh24:mi:ss') <= '07:30:59'
or
EXTRACT(MONTH FROM CAST(Login_time AS TIMESTAMP)) = 8
and to_char(login_time, 'fmSSSSS') between 6*60*60 + 30*60 AND 7*60*60 + 30*60
I've taken a different approach to the "between 6:30 AM and 7:30 AM" requirement. In this answer, I subtract 30 minutes, then check if the hour value is either 6 or 7. I chose this approach as a variation to string comparison. I don't know if it performs better but it's an alternate approach. The casting to timestamp is necessary to extract the hour value.
WHERE trunc(login_time) BETWEEN to_date('8/1/2018', 'mm/dd/yyyy') AND to_date('8/31/2018', 'mm/dd/yyyy')
AND extract(hour FROM cast(login_time-(30/60/24) as timestamp)) IN (6,7)
If you are always going to be querying an entire month, this could be shortened to
WHERE trunc(login_time, 'MONTH') = to_date('8/1/2018', 'mm/dd/yyyy')
AND extract(hour FROM cast(login_time-(30/60/24) as timestamp)) IN (6,7)

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.

How to “group by” over a DATETIME range?

I'm trying to bulid up a datetime range based transactions report, for a business that can be open across two days, depending on the shift management.
The user can select a datetime range (monthly, daily, weekly, freely...), the query I implemented get the startDateTime and the EndDateTime, and will return all the transactions total grouped by day.
I.E.
DateTime Total Sales
---------------------------
10/15/2010 $2,300.38
10/16/2010 $1,780.00
10/17/2010 $4,200.22
10/20/2010 $900.66
My problem is that if the shift of the business is setted, for example, from 05.00 AM to 02.00 AM of the next day, all the transactions done from midnight to 02.00 AM will be grouped in the next day... and so on... the totals are corrupted.
When a business has a shift like this, it wants a report based on that shift, but without code patching (I'm using Java calling Oracle native queries), I'm unable to get the requested report.
I'm wondering if there is some smart manner to group by a datetime range these sets of transactions using nothing more than Oracle.
Here goes the query, for the the month of July:
SELECT Q1.dateFormat, NVL(Q1.sales, 0)
FROM (
SELECT to_date(to_char(tx.datetimeGMT +1/24 , 'mm-dd-yyyy'), 'mm-dd-yyyy') AS dateFormat
, NVL(SUM(tx.amount),0) AS sales
FROM Transaction tx
WHERE tx.datetimeGMT > to_date('20100801 08:59:59', 'yyyymmdd hh24:mi:ss') +1/24
AND tx.datetimeGMT < to_date('20100901 09:00:00', 'yyyymmdd hh24:mi:ss') + 1/24
GROUP BY to_date(to_char(tx.datetimeGMT +1/24 , 'mm-dd-yyyy'), 'mm-dd-yyyy')
) Q1
ORDER BY 1 DESC
Thank you all for your answers, by taking a look to them I could write down the query I was searching for:
SELECT CASE
WHEN EXTRACT(HOUR FROM TX.DATETIME) >= 5 THEN TO_CHAR(TX.DATETIME,'DD-MM-YYYY')
WHEN EXTRACT(HOUR FROM TX.DATETIME) BETWEEN 0 AND 2 THEN TO_CHAR(TX.DATETIME-1,'DD-MM-YYYY')
WHEN EXTRACT(hour from tx.datetime) between 2 and 5 THEN to_char(TX.DATETIME-1,'DD-MM-YYYY')
END AS age,
NVL(SUM(tx.amount),0) AS sales
FROM TRANSACTION TX
WHERE tx.datetime > to_date('20100801 08:59:59', 'yyyymmdd hh24:mi:ss')
AND TX.DATETIME < TO_DATE('20100901 09:00:00', 'yyyymmdd hh24:mi:ss')
GROUP BY CASE
WHEN EXTRACT(HOUR FROM TX.DATETIME) >= 5 THEN TO_CHAR(TX.DATETIME,'DD-MM-YYYY')
WHEN EXTRACT(HOUR FROM TX.DATETIME) BETWEEN 0 AND 2 THEN TO_CHAR(TX.DATETIME-1,'DD-MM-YYYY')
WHEN EXTRACT(hour from tx.datetime) between 2 and 5 THEN to_char(TX.DATETIME-1,'DD-MM-YYYY')
END
ORDER BY 1
To group by a date range, you'll have to have this range into a column value into a subquery, and group by it in your query. Obviously, this date range within this column value will be of VARCHAR type.
If the first shift of the day starts at 08:00, and the last shift of that same day ends 07:59 the next day, you can use something like this to group the transactions by the shift date.
select trunc(trans_date - interval '8' hour) as shift_date
,sum(amount)
from transactions
group
by trunc(trans_date - interval '8' hour)
order
by shift_date desc;
You can try this approach (just out of my head, not even sure if it runs):
select
trans_date,
trans_shift,
aggregates(whatever)
from (
select
-- we want to group by normalized transaction date,
-- not by real transaction date
normalized_trans_date,
-- get the shift to group by
case
when trans_date between trunc(normalized_trans_date) + shift_1_start_offset and
trunc(normalized_trans_date) + shift_1_end_offset then
1
when trans_date between trunc(normalized_trans_date) + shift_2_start_offset and
trunc(normalized_trans_date) + shift_2_end_offset then
2
...
when trans_date between trunc(normalized_trans_date) + shift_N_start_offset and
trunc(normalized_trans_date) + shift_N_end_offset then
N
end trans_shift,
whatever
from (
select
-- get a normalized transaction date: if date is before 1st shift
-- it belongs to the day before
case
when trans_date - trunc(trans_date) < shift_1_start_offset then
trans_date - 1
else
trans_date
end normalized_trans_date,
t.*
from
transactions t
)
)
group by trans_date, trans_shift
Ronnis solution with the trunc(trans_date - interval '8' hour) helped me in a similar query.
Did a Backup Report and had to summarize output-bytes from RC_BACKUP_SET_DETAILS. The backup task runs for more than 8 hours, there are several RC_BACKUP_SET_DETAILS rows for one job which starts at night time and end the next day.
select trunc(start_time - interval '12' hour) "Start Date",
to_char(sum(output_bytes)/(1024*1024*1024),'999,990.0') "Output GB"
from rc_backup_set_details
where db_key = 173916 and backup_type = 'I' and incremental_level = 0
group by trunc(start_time - interval '12' hour)
order by 1 asc;

Resources