Search Last 7 days excluding today Oracle SQL - sql

I have the below code to which I want to return the last 7 days excluding today (for example from 5th May - 11th May as opposed to 5th May - 12th May)
What else would I be able to include to acheive this?
SELECT *
FROM TABLE_1
WHERE DATE_TIME >= SYSDATE -7

You want to have a range that starts from 7 days before midnight today and ends before midnight today:
SELECT *
FROM table_name
WHERE date_time >= TRUNC(sysdate) - 7
AND date_time < TRUNC(sysdate);

This should work:
SELECT *
FROM TABLE_1
WHERE DATE_TIME >= SYSDATE -7
AND TRUNC(DATE_TIME) != TRUNC(SYSDATE)
The TRUNC is needed to strip the time portion of the date column and sysdate.
Note that DATE_TIME >= SYSDATE -7 will include the time portion of SYSDATE and substract 7 days. If you run the query at 10AM, do you want to include rows that have date_time = sysdate - 7 at 9AM too ? If so it is better to add a TRUNC there too DATE_TIME >= TRUNC(SYSDATE) -7.

Related

Bring data from the last day of the previous month beyond

I'm using Oracle SQL Developer and I would like to make a SELECT that brings all the products sold from the last day of the previous month (but only products sold from 4 pm on the last day of the previous month) to the current day (only until 8 am of the current day).
For example, today is 7/21/2022. If I run this query today, it should bring data from:
06/30/2022 above 16:00hrs -> 07/21/2022 until 08:00hrs
You can use TRUNC(value, 'MM') to find midnight of the 1st day of the current month and then subtract 8 hours to find the start of the range and then use TRUNC(value) to find midnight of today and add 8 hours to find the end of the range:
SELECT *
FROM table_name
WHERE date_column >= TRUNC(SYSDATE, 'MM') - INTERVAL '8' HOUR
AND date_column <= TRUNC(SYSDATE) + INTERVAL '8' HOUR;
You can use TRUNC to get to the first day of a date's month. Then subtract one day and add sixteen hours. And it's again TRUNC that you use to get back to the beginning of a day (midnight) to which you can add eight hours.
select *
from mytable
where dt >= trunc(sysdate, 'mm') - interval '1' day + interval '16' hour
and dt < trunc(sysdate, 'dd') + interval '8' hour
order by dt;

Date filtering from past two weeks

I have an SQL query where I'm trying to select only records from the past two weeks of the present day i.e. using the 'created_on' column. Any ideas how that is done? I know I can't use specific dates because present day is always changing.
select un.id
, t.type_name as type
, un.content_id
, un.app_link
, un.notification_text
, t.id as type_id
, un.seen_yn,un.created_on
, to_char(un.created_on,'YYYY-MM-DD: HH24:MI') as timestamp
from app.user_notifications as un
left join ref.types as t on t.id = un.notification_type
where un.active_yn = true
and un.user_id = 1
and un.seen_yn = false order by un.created_on desc
You can compare the created_on column with an expression that calculates "two weeks before today":
un.created_on >= current_date - interval '2 weeks'
alternatively you can also subtract 14 days
un.created_on >= current_date - 14
Add yet another condition, e.g.
... and un.created_on >= trunc(sysdate) - 14
In Oracle, when you subtract number from a DATE datatype value, you subtract that many days so - subtracting 14 is two weeks back.
SQL> select sysdate right_now,
2 trunc(sysdate) today_midnight,
3 trunc(sysdate) - 14 two_weeks_back
4 from dual;
RIGHT_NOW TODAY_MIDNIGHT TWO_WEEKS_BACK
------------------- ------------------- -------------------
22.03.2022 07:12:54 22.03.2022 00:00:00 08.03.2022 00:00:00
SQL>
Alternatively, if you prefer interval (which actually says what you're doing), then
SQL> select trunc(sysdate) - interval '14' day from dual;
TRUNC(SYSDATE)-INTE
-------------------
08.03.2022 00:00:00
SQL>

Need last 30 days of data in library database

I need the magazines that were added to this database for the last 30 days. I have tried the following:
SELECT A.TITLE, A.EDITION, B.DONATEDBY, B.DATE_ADDED
FROM LIBRARY A
JOIN LIB_COPIES B ON A.IDNO=B.IDNO
WHERE A.TYPE LIKE '%Magazine%'
AND B.DONATEDBY LIKE '%State%'
AND B.DATE_ADDED>CURRENT_DATE-31
ORDER BY B.DATE_ADDED
It doesn't get any entries from today's date.
I have tried
DATEDIFF(dateColumn, CURRENT_TIMESTAMP) BETWEEN 0 AND 30
AND >= DATEADD(day, -30, GETDATE())
I am new to this forgive me if in wrong format. I put B.Date_added for that is my column, where I felt it should be.
Please help me!
Assuming you're using MySQL, CURRENT_DATE-31 does not subtract 31 days from the current date. Instead it converts CURRENT_DATE to an integer and subtracts 31.
select CURRENT_DATE, CURRENT_DATE - 31;
CURRENT_DATE CURRENT_DATE - 31
2020-01-10 20200079
Instead, use date_sub(current_date, interval 31 day).
select CURRENT_DATE, date_sub(CURRENT_DATE, interval 31 day);
CURRENT_DATE date_sub(CURRENT_DATE, interval 31 day)
2020-01-10 2019-12-10
Or if you mean 1 month, use interval 1 month.
DATEDIFF(dateColumn,CURRENT_TIMESTAMP) BETWEEN 0 AND 30 does not work because when dateColumn is in the past the diff will be negative.
select DATEDIFF("2020-01-01",current_date);
DATEDIFF("2020-01-01",current_date)
-9
Instead, reverse the order of the dates.
select DATEDIFF(current_date, "2020-01-01")
DATEDIFF(current_date, "2020-01-01")
9

Querying last 5 years

I want to query all products sold in the last 5 years.
It is possible to do it like this:
select * from products
where time between sysdate-1826 and sysdate
But it there also a nicer way instead of calculating all the days and subtract it from sysdate?
SELECT *
FROM products
WHERE date_column >= add_months( sysdate, -12*5 )
or
SELECT *
FROM products
WHERE date_column >= sysdate - interval '5' year
will both give you all the rows from the last 5 years (though you would generally want to add a TRUNC to remove the time portion unless you really care whether a row was created on Feb 8, 2007 in the morning or in the afternoon).
select * from products
where time > DATE_SUB(NOW(), INTERVAL 5 YEAR)
Date sub will subtract 5 years from now

Oracle SQL Where Clause against a date column

I have a DATE column with a date in it but I need to query it to find records less than 30 days old.
START_DATE
----------
01-AUG-2010
09-AUG-2010
22-AUG-2010
09-SEP-2010
Query:
SELECT START_DATE
WHERE START_DATE < 30;
I know it is simple Query in ORACLE SQL but i am doing something wrong.
Use:
SELECT t.start_date
FROM YOUR_TABLE t
WHERE t.start_date > SYSDATE - 30
SYSDATE is Oracle's syntax to get the current date and time
In Oracle, you can do date arithmetic in the context of days, so SYSDATE - 30 means "current date, subtract thirty days" to get a date that is thirty days in the past
If you want to evaluate the date based on thirty days as of midnight, use the TRUNC function:
SELECT t.start_date
FROM YOUR_TABLE t
WHERE t.start_date > TRUNC(SYSDATE) - 30
Don't run TRUNC on the column - that will render an index on the column useless, ensuring a table scan.
SELECT t.start_date
FROM YOUR_TABLE t
WHERE t.start_date > SYSDATE - INTERVAL '30' DAY;
INTERVAL is more portable than assuming that you can add or subtract days, although I've noticed some slight differences in the INTERVAL syntax between Oracle and PostgreSQL.
WHERE START_DATE > SYSDATE - 1
or perhaps
WHERE TRIM(STARTDATE) > TRIM(SYSDATE) - 1