How can I get data of the current week in Oracle - oracle

How can I get data of the current week in Oracle? Assume that the week goes from Sunday to Saturday
SELECT * FROM TABLENAME
WHERE DATEFIELD IS {WITHIN CURRENT WEEK THAT GOES FROM 12AM ON SUNDAY TO 11:59PM ON SATURDAY}

Use Oracle trunc(sysdate,'IW') returns the first day of the week and add 6 to get the end of week.
-- not tested
SELECT * FROM TABLENAME
WHERE DATEFIELD >= trunc ( sysdate, 'iw' )
AND DATEFIELD < trunc ( sysdate, 'iw' ) + 5

Related

Oracle: splitting time range into days and calculation of duration

I'm developing code calculation service availability based on events, so I need to split events into daily "sub-events" and calculate duration of then.
So as input I have set of events like (EVENT_ID, START_TIME, END_TIME):
'event1';2021-05-01 12:30;2021-05-01 13:00
'event2';2021-05-03 10:55;2021-05-05 12:01
As output I'd like to get (EVENT_ID, DAY, DURATION_MINUTES):
'event1'; 2021-05-01; 30
'event2'; 2021-05-03; 785
'event2'; 2021-05-04; 1440
'event2'; 2021-05-05; 721
I can get it using procedures and cursor but this is not effective (the events database is quite big), so is there a way to do it using oracle sql query ? Any idea?
You appear to want a recursive query:
WITH days ( event_id, day, start_time, end_time ) AS (
SELECT event_id,
TRUNC( start_time ),
start_time,
end_time
FROM table_name
UNION ALL
SELECT event_id,
day + INTERVAL '1' DAY,
start_time,
end_time
FROM days
WHERE day + INTERVAL '1' DAY < end_time
)
SELECT event_id,
day,
ROUND(
(
LEAST(end_time, day + INTERVAL '1' DAY)
- GREATEST(start_time, day)
) * 24 * 60
) AS duration_minutes
FROM days
Which, for the sample data:
CREATE TABLE table_name ( event_id, start_time, end_time ) AS
SELECT 'event1', DATE '2021-05-01' + INTERVAL '12:30' HOUR TO MINUTE, DATE '2021-05-01' + INTERVAL '13:00' HOUR TO MINUTE FROM DUAL UNION ALL
SELECT 'event2', DATE '2021-05-03' + INTERVAL '10:55' HOUR TO MINUTE, DATE '2021-05-05' + INTERVAL '12:01' HOUR TO MINUTE FROM DUAL;
Outputs:
EVENT_ID
DAY
DURATION_MINUTES
event1
2021-05-01
30
event2
2021-05-03
785
event2
2021-05-04
1440
event2
2021-05-05
721
db<>fiddle here
If your Oracle version is 12 or higher, you can use a lateral join (in any of several equivalent formulations/syntaxes) to make the query faster. For example (using the table set up in MT0's answer):
select event_id, day, round(1440 * duration_days) as duration_minutes
from table_name cross join lateral
( select trunc(start_time) + level - 1 as day,
case when level = 1 and connect_by_isleaf = 1
then end_time - start_time
when level = 1 then 1 - (start_time - trunc(start_time))
when connect_by_isleaf = 1 then end_time - trunc(end_time)
else 1 end as duration_days
from dual
connect by level <= 1 + trunc(end_time) - trunc(start_time)
)
where duration_days != 0
order by event_id, day
;
The where clause is used when the end_time is midnight (at the beginning of an otherwise "new" day); in that case, presumably, you don't want to include that "new day" in the output, with a duration of 0 minutes.
In the lateral view, level = 1 corresponds to the first date in the interval, while connect_by_isleaf = 1 is for the last date in the interval. A special calculation is made when the end_time and start_time are on the same date. The query computes the difference in days first, then converts to minutes. Note that date calculations aren't 100% precise; I used round so I don't get results like 33.9999999999938020 minutes. If the inputs are in hh24:mi, we know beforehand that the answer (in minutes) should be an integer, so round seems fine there.

how to get previous month or last two month data in oracle

how to get previous month or last two month data in oracle.
My date format is YYYY,MM,DD.
from google search i got those solution,
select * from IM_LAPTOP
where ADD_DATE >= add_months(sysdate, -12);
select *
from IM_LAPTOP
where ADD_DATE between add_months(trunc(sysdate,'mm'),-1) and last_day(add_months(trunc(sysdate,'mm'),-1));
But its showing not a valid month
My date format is YYYY,MM,DD
Unless you are using a string to store dates then, no, it is not; a DATE is a binary data type (consisting of 1-byte for each of: century, year-of-century, month, day, hour, minute and second and it always has those components) and it has no format.
how to get previous month or last two month data in oracle
To get the data from 2 months before this instant in time (i.e. if it is now 2021-04-05 16:39:24 and you want it from 2021-02-05 16:39:24, two months prior) then:
SELECT *
FROM your_table
WHERE date_column >= ADD_MONTHS( SYSDATE, -2 )
To get the data starting from midnight on the 1st day of last month:
SELECT *
FROM your_table
WHERE date_column >= ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), -1 )
If you only want the data from the preceding month then:
SELECT *
FROM your_table
WHERE date_column >= ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), -1 )
AND date_column < TRUNC( SYSDATE, 'MM' )
If your "date" is actually a VARCHAR2 column with the format YYYY,MM,DD then you should change it to a DATE column but if for some reason you cannot then at least the characters are in order of highest-to-least significance and you can perform an alphanumeric comparison and just wrap the right-hand side of the filters in TO_CHAR:
SELECT *
FROM your_table
WHERE date_column >= TO_CHAR( ADD_MONTHS( SYSDATE, -2 ), 'YYYY,MM,DD' )

Return records from todays month + 6

how can i return records sales from todays + 6 months.
Example:
Today´s month: May-2018.
Today´s month + 6: Nov-2018
So, i want a function to retrieve records from Table_Date, from November (1 to 30).
Thanks.
Use the TRUNC and ADD_MONTHS functions:
SELECT *
FROM sales
WHERE SalesDate >= ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), 6 )
AND SalesDate < ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), 7 );
You have SQL server and oracle, it cant be both.
This is for SQL Server (though logic would work in ORACLE, but syntax may not)
This is to get 6 months from today
SELECT DATEADD(MM, 6, GETDATE())
Added extra code here for more details after looking at question more:
I am using variables to do the calculations to make it easier to understand but you could do it all inline (but harder to read).
This script gets a date 6 months from now, takes that month and finds the first day of that month, and then the last day of that month so you can use those 2 dates in a where clause logic.
DECLARE #NewMonth AS VARCHAR(10)
DECLARE #NewYear AS VARCHAR(10) -- in case adding months goes to next year
SELECT #NewMonth = DATEPART(MONTH, DATEADD(MM, 6, GETDATE())),
#NewYear = DATEPART(YEAR, DATEADD(MM, 6, GETDATE()))
DECLARE #FirstOfMonth AS DATE = #NewMonth + '/1/' + #NewYear
-- for start of month check we can just hard code one in it, for end of month we cant because we dont know how many days are in it
SELECT #FirstOfMonth, DATEADD(DAY, -1, DATEADD(MONTH, 1, #FirstOfMonth)) AS LastDayOfMontbh

Oracle sysdate flexible date

I worked this statement out
SELECT to_date('30.06.2016', 'dd.mm.yyyy') - (LEVEL-1) DATUM
FROM DUAL
CONNECT BY LEVEL <= 366;
which gives me all dates from 30.06.2016 till 366 days in the past.
So far so good.
What I need to add is that to_date('30.06.2016') is more flexible..
What I mean I always want it to use the last day of June in sysdate + 1 year.
In this case we have 2015 at the moment - so we have 30.06.2016.
If we had 2016 I need it to use 30.06.2017.
If we had 2017 I need it to use 30.06.2018.
..
..
Thanks for your help.
EDIT Solution:
SELECT last_day(add_months(to_date('01.06.' || to_char(sysdate, 'YYYY'), 'dd.mm.yyyy'),12)) - (LEVEL-1) DATUM
FROM DUAL
CONNECT BY LEVEL <= 366
If you want 366 days worth of dates:
SELECT TRUNC( SYSDATE, 'YEAR' ) + INTERVAL '18' MONTH - LEVEL AS DATUM
FROM DUAL
CONNECT BY LEVEL <= 366;
Or if you want a year's worth (365 days or 366 days in a leap year) of dates (1st July this year to 30th June next year):
SELECT TRUNC( SYSDATE, 'YEAR' ) + INTERVAL '18' MONTH - LEVEL AS DATUM
FROM DUAL
CONNECT BY TRUNC( SYSDATE, 'YEAR' ) + INTERVAL '18' MONTH - LEVEL >= TRUNC( SYSDATE, 'YEAR' ) + INTERVAL '6' MONTH;
Is your same code, but get from sysdate the year, using to_char:
select to_date('30.06.'||(to_char(sysdate,'yyyy')+1),'dd.mm.yyyy') from dual;
Here's the steps:
Truncate sysdate to the year, using Trunc().
Add 18 months, using Add_Months().
Subtract one day.

How to check if a customer's birthday has passed for the ongoing year?

I have a table storing information about customers which includes a column "dateofbirth". I need to fetch those customers whose birthday has passed for the ongoing year (2015). How can I extract both date and month and compare it with system date and month?
You can use EXTRACT:
SELECT *
FROM CUSTOMERS
WHERE EXTRACT( MONTH FROM dateofbirth ) < EXTRACT( MONTH FROM SYSDATE )
OR ( EXTRACT( MONTH FROM dateofbirth ) = EXTRACT( MONTH FROM SYSDATE )
AND EXTRACT( DAY FROM dateofbirth ) <= EXTRACT( DAY FROM SYSDATE ) )
or you can use TO_CHAR with the MMDD day of year format
SELECT *
FROM CUSTOMERS
WHERE TO_CHAR( dateofbirth, 'MMDD' ) <= TO_CHAR( SYSDATE, 'MMDD' )
select to_char(to_date('27-01-1966','DD-MM-YYYY'),'MMDD'),to_char(sysdate,'MMDD'),
CASE WHEN to_char(to_date('27-01-1966','DD-MM-YYYY'),'MMDD') > to_char(sysdate,'MMDD')
THEN 'No'
ELSE 'Yes'
END
from dual;
So
WHERE to_char(dateofbirth,'MMDD') < to_char(sysdate,'MMDD')
I suggested the following solution
SELECT
name,
TO_CHAR(dob,'mm') MONTH,
TO_CHAR(dob,'dd') DAY
FROM
target_table
WHERE
TO_CHAR(sysdate,'yyyy') =2015
AND
(
to_number(TO_CHAR(dob,'mm'))<to_number(TO_CHAR(sysdate,'mm'))
OR
(
TO_CHAR(dob,'mm') =TO_CHAR(sysdate,'mm')
AND TO_CHAR(dob,'dd')<=TO_CHAR(sysdate,'dd')
)
);
i had written code based on my table data......
dob=date of birth
tartget_table= required table

Resources