Home » SQL & PL/SQL » SQL & PL/SQL » Results Set based on Dynamic Date stored in variable (Oracle SQL Developer)
Results Set based on Dynamic Date stored in variable [message #644758] Mon, 16 November 2015 15:50 Go to next message
Anil98765
Messages: 3
Registered: November 2015
Location: Earth
Junior Member
Hi, I'm very new to the Oracle world coming from a SQL background. I could do this in SQL Server.

Basically, I created 2 variable for Start/End date to pull the previous Monday thru Sunday. (Example if today is 11/16 - I'd pull 11/9 - 11/15). I was to dynamically get those dates and assign it to the PrevWeekStateDate & PrevWeekEndDate to use them in the where condition.



--Declare Variables 
variable  PrevWeekStateDate varchar2(30);
variable  PrevWeekEndDate  varchar2(30);

--Hardcodes date - I dont want this dynamically get the dates
exec  :PrevWeekStateDate :='11022015';
exec  :PrevWeekEndDate :='11082015';
 
/*  How do I assign the select to the var?
--Set Variables bases on current date to get previous Monday and previous Sunday
exec  :PrevWeekStateDate :=  (select next_day (sysdate-8,'MONDAY')  from dual);
exec  :PrevWeekEndDate :=   (select next_day (sysdate-7, 'SUNDAY')  from dual);

--2nd attempt to assign via into
--Set Variables bases on current date to get previous Monday and previous Sunday
select next_day (sysdate-8,'MONDAY') into PrevWeekStateDate from dual;
select next_day (sysdate-7, 'SUNDAY') into PrevWeekEndDate from dual;
*/

--Get results set
SELECT 
    COUNT(*)
 FROM worklight.TBL_ACTIVITY_LOG
WHERE ACTIVITY LIKE '%Refill Proc%'
AND activity_dttm between to_date(:PrevWeekStateDate, 'mmddyyyy')
AND to_date(:PrevWeekEndDate, 'mmddyyyy');




Any insight would be great as I started a new job and trying to impress the boss by getting this scheduled.

Re: Results Set based on Dynamic Date stored in variable [message #644759 is a reply to message #644758] Mon, 16 November 2015 17:52 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
SELECT Count(*) 
FROM   worklight.tbl_activity_log 
WHERE  activity LIKE '%Refill Proc%' 
       AND activity_dttm BETWEEN Next_day (SYSDATE - 8, 'MONDAY') AND 
                                 Next_day (SYSDATE - 7, 'SUNDAY'); 
Re: Results Set based on Dynamic Date stored in variable [message #644781 is a reply to message #644758] Tue, 17 November 2015 06:52 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
Quote:

--Declare Variables
variable PrevWeekStateDate varchar2(30);
variable PrevWeekEndDate varchar2(30);

There's your first mistake. Dates should be DATE, not VARCHAR2. Make all your date of data type DATE, and the rest almost becomes trivial.

Quote:

exec :PrevWeekStateDate :='11022015';

then you compound the problem by providing your "date" (actually, just a character string) in what appears to be mmddyyyy format. So what is greater, '12151952' or '01012015'? Since they are just strings, a comparison is going to say that the 1952 "date"is greater (more recent) than the 2015 "date".

see: But I want to store my date as ...
(And this principle of using the correct data type holds regardless of the database product you are using. I know that SQL Server has some different data types dealing with date and time. but the principle of NOT using character strings still holds. Your treatment of dates is just as bad in SQL Server as it is in Oracle.


And on the same block of code -- that's not how you assign a value to a variable. 'exec' is a sql command to execute a procedure.



Quote:

Any insight would be great as I started a new job and trying to impress the boss by getting this scheduled.


So you accepted a job writing Oracle PL/SQL, and you've never written Oracle PL/SQL? And you want to impress your boss? If I were your boss I'd be more impressed by a bit honesty at this point.

Have you even looked at the SQL Reference and PL/SQL references? It appears not. It appears you are now just asking the forum to do your job for you.
Re: Results Set based on Dynamic Date stored in variable [message #644783 is a reply to message #644781] Tue, 17 November 2015 11:49 Go to previous messageGo to next message
Anil98765
Messages: 3
Registered: November 2015
Location: Earth
Junior Member
BlackSawn, Thanks for the response. The
Next_day (SYSDATE - 8, 'MONDAY') AND Next_day (SYSDATE - 7, 'SUNDAY')
returned 0 results to tried
 Next_day (SYSDATE - 15, 'MONDAY') AND   Next_day (SYSDATE - 9, 'SUNDAY');
which retured me 642 which is 3 off the from the harded coded dates with a record count 645.

Thanks for getting me 1 step closer & will keep working on it.

Sorry if I was asking for too much help as EdStevens indicate, but in SQL Server it would of been quite simple for me, so didn't think I was asking for your first born or anything. Everyone needs to starts somewhere, eh.

Much appeciated.

Re: Results Set based on Dynamic Date stored in variable [message #644784 is a reply to message #644783] Tue, 17 November 2015 12:24 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>which retured me 642 which is 3 off the from the harded coded dates with a record count 645.
My gut suspect that the missing 3 are boundary edge rows; which may or may be correct.
Re: Results Set based on Dynamic Date stored in variable [message #644785 is a reply to message #644784] Tue, 17 November 2015 13:20 Go to previous messageGo to next message
Anil98765
Messages: 3
Registered: November 2015
Location: Earth
Junior Member
The 642 was returned using the dynamic dates of Next_day (SYSDATE - 15, 'MONDAY') AND Next_day (SYSDATE - 9, 'SUNDAY'), but using hardcoded values of '11/09/2015' and '11/15/2015'. (Example-activity_dttm between to_date('11092015', 'mmddyyyy') AND to_date('11152015', 'mmddyyyy')) it retured 645.

I was following your same assumption that they are borderline records so was trying to break out the dates (11/09/2015 23:59:000:000) to see if the 3 records fell on 11/09 or 11/15, but looks like the table/view I'm have access to just show the date.

So then I tried to dump the 645 and concatement the values to get a uniquie to vlookup the 3 missing records from the results set of 643.

Still a work in progess, but slowly getting used to everything in Oracle SQL Developer.
Re: Results Set based on Dynamic Date stored in variable [message #644786 is a reply to message #644785] Tue, 17 November 2015 13:32 Go to previous message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>but looks like the table/view I'm have access to just show the date.
TO_CHAR(SYSDATE,'YYYY-MM-DD HH24:MI:SS')

select <QUERY returns 645>
MINUS
select <QUERY returns 642>

above returns result set of "missing" rows
Previous Topic: Stored Proc not inserting all records
Next Topic: How to analyze complex sql queries
Goto Forum:
  


Current Time: Fri Apr 19 04:32:25 CDT 2024