Oracle SQL - Get records from current month last year - sql

I'm working with Oracle SQL.
I'm trying to get all records from the current month and last year.
Example: This is August 12, so I want all records from 1-12 August 2017
The query gets me all records in the current month 2018.
BETWEEN trunc (sysdate, 'mm') AND SYSDATE
How to do the same for last year?

You can use ADD_MONTHS function to get 12 months before:
Between trunc(ADD_MONTHS(sysdate,-12),'mm') and trunc(ADD_MONTHS(sysdate,-11),'mm')

You can do:
where date >= trunc(sysdate, 'mon') - interval '12' month and
date < trunc(sysdate, 'mon') - interval '11' month

Related

How can I get the previous month data in ORACLE, if I have to run the sql script every month?

Here is my version of SQL code:
select HISTORY_ID,START_DTM FROM LEM.LEM_ALERT_HISTORY
where MONTH from START_DTM = add_months (sysdate, -1) ;
Also, what if the current month is Jan, how can I get the data from last year's Dec?
Thanks a lot
You can try this one:
select HISTORY_ID
, START_DTM
from LEM.LEM_ALERT_HISTORY
where START_DTM >= TRUNC(ADD_MONTHS(SYSDATE, -1),'MM') AND START_DTM < LAST_DAY(ADD_MONTHS(TRUNC(SYSDATE,'mm'),-1))
If the current month is January it will return the data from December last year

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;

Select rows where max date is less than December 31st of the previous year

I am using Oracle SQL developer. I am trying to select rows where max date is less than December 31st of the previous year. I tried this query below but kept getting an error.
Select ORG_ID, STS_DTE
from Table1
Group By RC_ORG_ID
Having MAX(STS_DTE)< '31-Dec-' || extract (year from SYSDATE-1)
I would use trunc():
having MAX(STS_DTE) < trunc(sysdate, 'YYYY') - interval '1 day'
I'm not sure if you want sysdate - interval '1' day.

Oracle SQL - Define the year element of a date dependent on the current month

I am trying to create a view in SQL Developer based on this statement:
SELECT * FROM ORDERS WHERE START_DATE > '01-JUL-2020'
The year element of the date needs to set to the year of the current date if the current month is between July and December otherwise it needs to be the previous year.
The statement below returns the required year but I don't know how to incorporate it (or a better alternative) into the statement above:
select
case
when month(sysdate) > 6 then
year(sysdate)
else
year(sysdate)-1
end year
from dual
Thanks
Oracle doesn't have a built-in month function so I'm assuming that is a user-defined function that you've created. Assuming that's the case, it sounds like you want
where start_date > (case when month(sysdate) > 6
then trunc(sysdate,'yyyy') + interval '6' month
else trunc(sysdate,'yyyy') - interval '6' month
end)
Just subtract six months and compare the dates:
SELECT *
FROM ORDERS
WHERE trunc(add_months(sysdate, -6), 'YYYY') = trunc(start_date, 'YYYY')
This compares the year of the date six months ago to the year on the record -- which seems to be the logic you want.

getting last day from next month

I need to get last day from next month in this select.
select (last_day(month from :date+1)||'.'||
(extract(month from :date)+1)||'.'||
extract(year from :date))
from dual;
Everything is ok with month and year but I have a problem with last day function.
Using (extract(month from :date)+1) will not work for a date in December as you will end up with a 13th month. Similarly, extract(year from :date) will get you the current year - which may not be correct if you are looking for the last day of the next month from December and the result should be the January of the following year.
You do not need to extract all the fields separately, you can do it all in a single statement:
SELECT TO_CHAR(
LAST_DAY( ADD_MONTHS( :date, 1 ) ),
'DD.MM.YYYY'
)
FROM DUAL