Home » SQL & PL/SQL » SQL & PL/SQL » To get Last week ?
To get Last week ? [message #603009] Tue, 10 December 2013 23:46 Go to next message
malli123
Messages: 6
Registered: December 2013
Junior Member
Hi,

I am using the below function for getting last week.

(current_date-DAYOFWEEK(Current_Date)) +1

If i run in any date it will bring the respective last week.

Can any one please help me above function is correct ?


[MERGED by LF]

[Updated on: Thu, 12 December 2013 02:09] by Moderator

Report message to a moderator

Re: To get Last week ? [message #603010 is a reply to message #603009] Tue, 10 December 2013 23:53 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
malli123 wrote on Tue, 10 December 2013 21:46
Hi,

I am using the below function for getting last week.

(current_date-DAYOFWEEK(Current_Date)) +1

If i run in any date it will bring the respective last week.

Can any one please help me above function is correct ?


yes, no, maybe, it depends given that only you know about CURRENT_DATE & DAYOFWEEK; since neither are Oracle standard objects.
Re: To get Last week ? [message #603011 is a reply to message #603010] Tue, 10 December 2013 23:57 Go to previous messageGo to next message
malli123
Messages: 6
Registered: December 2013
Junior Member
Cool !

I am getting '12/8/2013' result but it is this week but i need last week(01/12/2013).
Re: To get Last week ? [message #603012 is a reply to message #603011] Wed, 11 December 2013 00:06 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
SELECT SYSDATE-7 FROM DUAL;
Re: To get Last week ? [message #603019 is a reply to message #603011] Wed, 11 December 2013 00:37 Go to previous messageGo to next message
rleishman
Messages: 3728
Registered: October 2005
Location: Melbourne, Australia
Senior Member
select to_char(trunc(current_date, 'W') - 7, 'YYYY-MM-DD')
from dual


CURRENT_DATE is standard in Oracle. It is like SYSDATE except it uses the session timezone instead of the database timezone. I don't think Oracle has a DAYOFWEEK() function; the closest equivalent might be TO_DATE(..., 'D')

It's possible that truncating to the beginning of the week might be impacted by your locale. Some locales (France maybe?) have Monday as the start of the week; but most (according to me) seem to be Sunday. I believe this affects the results to TO_CHAR(sysdate, 'D') (day as a digit: 1-7), it might well affect TRUNC(date, 'W') as well. I leave it as an exercise for you to find out and decide whether it impacts your program.

Ross Leishman
Re: To get Last week ? [message #603051 is a reply to message #603019] Wed, 11 December 2013 03:45 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3270
Registered: January 2010
Location: Connecticut, USA
Senior Member
Well, did you check Oracle docs? TRUNC(date, 'W') is NLS independent since it format element W assumes week 1 starts on the first day of the month and ends on the seventh. So I doubted OP wnats that. Oracle equivalent of:

(current_date-DAYOFWEEK(Current_Date)) +1


which, btw, is first day of current week nd not last week, would be:

trunc(sysdate) - to_char(sysdate,'D') + 1


And it is NLS depenedent. So client in US will get December 8 while client in France December 9:

SQL> alter session set nls_territory=America;

Session altered.

SQL> select sysdate - to_char(sysdate,'D') + 1 from dual;

SYSDATE-T
---------
08-DEC-13

SQL> alter session set nls_territory=France;

Session altered.

SQL> select sysdate - to_char(sysdate,'D') + 1 from dual;

SYSDATE-
--------
09/12/13

SQL>


SY.
How to bring last week by current date ? [message #603164 is a reply to message #603009] Wed, 11 December 2013 23:06 Go to previous messageGo to next message
malli123
Messages: 6
Registered: December 2013
Junior Member
I need to create last week by using current_date. My week start from Monday and End on Sunday.
For example if i run my query this week(9/12/2013 to 15/12/2013) my current date should show last week end date(8/12/2013).

Can Some one help me on this ?

Thanks in advance !!
Re: How to bring last week by current date ? [message #603167 is a reply to message #603164] Wed, 11 December 2013 23:49 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Choose a proper format element in the SQL to get the last day of previous week. Have a look at http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34924.
And post what have you tried.
Re: How to bring last week by current date ? [message #603170 is a reply to message #603167] Thu, 12 December 2013 00:05 Go to previous messageGo to next message
malli123
Messages: 6
Registered: December 2013
Junior Member
i am using (current_date-dayofweek(current_date))+1, it is working fine from monday to saturday it always going last week end date(8/12/2013). But if i run same thing on Sunday(15/12/2013) it is not going last week.
Re: How to bring last week by current date ? [message #603172 is a reply to message #603170] Thu, 12 December 2013 00:23 Go to previous messageGo to next message
isri
Messages: 16
Registered: November 2012
Junior Member
In which version of Oracle dayofweek function appeared?

WITH cd AS 
(SELECT to_date('15122013','DDMMYYYY') AS cd FROM dual)
SELECT
TRUNC (cd, 'D') - 1
from cd

TRUNC(CD,'D')-1     
---------------------
2013/12/08 00:00:00
Re: How to bring last week by current date ? [message #603177 is a reply to message #603170] Thu, 12 December 2013 01:35 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
malli123 wrote on Thu, 12 December 2013 11:35
i am using (current_date-dayofweek(current_date))+1, it is working fine from monday to saturday it always going last week end date(8/12/2013). But if i run same thing on Sunday(15/12/2013) it is not going last week.


What is dayofweek? Is it a function you created? Or is it outside to Oracle where you are executing? In Oracle we do not have dayofweek function.
Re: How to bring last week by current date ? [message #603181 is a reply to message #603177] Thu, 12 December 2013 01:44 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
And isn't this the same thread?
Previous Topic: UPDATE table insert characters into a VARCHAR2 row
Next Topic: Problem In Accessing The value of Outer Cursor in inner Cursor ?
Goto Forum:
  


Current Time: Tue Apr 16 14:26:09 CDT 2024