Extract Sunday of the Week - sql

Hi Im working to extract the next Sunday of the week for any given date in PL/SQL.
I have the following code
select TRUNC(to_date('10-07-2016','mm-dd-yyyy'), 'w') + 8 - 1/86400
from dual;
This works good when the date itself does not fall on Sunday. When I change the date to 10/8, which is a Sunday, the result would be 10-15-2016, which is the next Sunday, while the desired result is 10-08-2016.
Any thought folks?

try
select TRUNC(to_date('10-09-2016','mm-dd-yyyy'), 'iw')+6 from dual;
'iw' returns the first day of the ISO week (Monday)

SELECT NEXT_DAY(TO_DATE('10-07-2016', 'mm-dd-yyyy')-8, 'SUN') FROM DUAL;

Related

Calculate Week Ending date in Oracle SQL where the week start is the previous SAT to the current FRI

I have a transaction date in my table that I would like to calculate a Week Ending date for
The problem is the weeks for us go from Saturday to Friday, don't ask me why,
So for example this week, today is 10/5/2020, the week end date should be 10/09/2020
and this Saturday, 10/10/2020, would be week ending date 10/16/2020.
Anyone know how to properly achieve this?
You could use next_day():
select t.*,
case when to_char(mydate, 'FMDAY') = 'FRIDAY'
then mydate
else next_day(mydate, 'FRIDAY')
end as endofweek
from mytable t
Note that both to_char(..., 'DAY') and next_day() are dependant on the language of your session. If your database or session are not in English language, you need to adujst the literal strings.

Oracle : Query to get the Last Friday of the week if I give any date

If I give a date let's say '13-Mar-2019' my query needs to retrieve the value '15-Mar-2019'. Which is the last Friday of the week.
Trancate the date to the ISO week, which gets you the week's Monday (as an ISO week starts with Monday). Then add four days:
select trunc(date '2019-03-13', 'iw') + 4 from dual;
I would use next_day(). It is the Oracle function specifically designed for this purpose.
select next_day(date '2019-03-13', 'Fri')
from dual;
The only nuance is that if the date is Friday, then it will return the next Friday. That might be what you want. Otherwise, just subtract one day:
select next_day(date '2019-03-13' - 1, 'Fri') as friday_end_of_week
from dual;
Try below -
select trunc(to_date('13-Mar-2019'), 'iw') + 4 from dual
SELECT NEXT_DAY( to_date('2019-03-13', 'yyyy-mm-dd'), to_char(to_date('2019-03-01', 'yyyy-mm-dd'), 'DAY')) FROM dual;
demo

In Oracle SQL, how to get the time for only this current week?

I have a query , where I want to obtain some data for different time durations (this month, this week, etc).
For the "this week" column, I want it to get all the data from the most recent Monday until now. How would I do this?
I have the following SQL so far :
WHERE prla.CREATION_DATE >= SYSDATE - ?
trunc(sysdate, 'iw') is what you're after. IW is the format mask used for Monday of the week the specified date is in (as Monday is the ISO standard for the start of the week). E.g.:
with dates as (select trunc(sysdate, 'mm') - 10 + level dt
from dual
connect by level <= 40)
select dt
from dates
where dt >= trunc(sysdate, 'iw')
and dt <= sysdate; -- only needed if the dates in the column could be beyond now.
Yeah that will do: But it is better to use sysdate-8. Because if the current day is same as your searching day, it will return the current date. For Eg.
select next_day(sysdate-7,'WED') from dual;
OUTPUT
19-AUG-15
Whereas the below one will give you the last week
select next_day(sysdate-8,'WED') from dual;
OUTPUT
12-AUG-15
You should truncate the current date.
TRUNC(SYSDATE, 'DAY')
This should give you the first day of the week, which is Monday in lot of countries.
If it's giving you the previous Sunday instead you should do this.
TRUNC(SYSDATE, 'DAY')+1
I found out to do this now:
select next_day (sysdate-7, 'MONDAY') Last_Monday from dual;
So in my case, we can remove the SYSDATE subtraction and it is simply :
prla.CREATION_DATE >= next_day (sysdate-7, 'MONDAY')
source

trunc / sysdate... funny syntax need explanation

I believe this outputs the date of Sunday of the current week but I don't know why.
Can someone please break down what's going on here.
SELECT trunc(sysdate+1,'DAY') FROM DUAL;
The TRUNC function will return the starting day of the week when the second parameter is DAY. Today Sysdate returns Thursday, +1 returns Friday etc. So when you add 3 it gives you next Sunday which marks the start of a new week.
Run this to understand what trunc does
SELECT to_char(trunc(sysdate+1, 'DAY'),'dd/mon/yyyy hh:mi:ss') FROM DUAL;
DAY does returns starting day of the week but trunc will also cut off the hours, minutes, seconds of that date. Sysdate will have some hours and minutes, but after trunc it is defaulted to 00.00.00.000
By calling this
trunc(sysdate+1,'DAY')
you may see 16-FEB-14. You can't see the real result because Oracle doesn't display the minutes for you. If you call this
SELECT to_char(sysdate+1,'dd/mon/yyyy hh:mi:ss') FROM DUAL;
you will see all the time details. Trunk takes that off.
In other words, you have 3 effects here - sysdate + 1 - next date, Day - first day of the week, Trunc - hours, minutes, seconds, etc. off
see http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions230.htm
'DAY' truncates the date provided to the starting day of the week.
Note that depending on your locale this can also be Monday instead of Sunday.
sysdate + 1 just adds one day to the current date, this is probably done to make sure if you are sunday you get back the current sunday and not the previous one.
select to_char(sysdate+1,'dd-mon-yy hh24:mi:ss') from dual;----22-feb-14 02:10:03
select to_char(trunc(sysdate+1,'year'),'dd-mon-yy hh24:mi:ss') from dual;-----01-jan-14 00:00:00
select to_char(trunc(sysdate+1,'month'),'dd-mon-yy hh24:mi:ss') from dual; ---01-feb-14 00:00:00
select to_char(trunc(sysdate+1,'day'),'dd-mon-yy hh24:mi:ss') from dual; ---16-feb-14 00:00:00
Compare above queries outputs, hope this will help you.

In oracle SQL, how would you obtain the timestamp representing the start of the week?

I'm using an Oracle 9i database and want to obtain, within a function, the timestamp representing the start of the week, i.e. The most recent monday, at 00:00:00.
I am aware that the timestamp representing the start of the current day is TO_TIMESTAMP(SYSDATE).
You can use the function next_day to get that:
SQL> select next_day(sysdate-7, 'MONDAY') FROM DUAL;
NEXT_DAY
---------
29-APR-13
Getting the start of the week should work with trunc (see docs).
So,
select to_timestamp(trunc(sysdate, 'D')) from dual
should work.
However, depending on your NLS settings, the first day of the week for oracle may well be Sunday.
this appears to return Monday before the day of week in question at midnight. to prove out just play around with sysdate and subtract days...
select case when to_Char(sysdate,'d') = 1 then
trunc(sysdate-6)
else
trunc(sysdate - (to_Char(sysdate,'d')-2))
END
from dual;
You can truncate a date to Monday with:
select trunc(sysdate, 'IW') FROM DUAL;