Solved

oracle date query

Posted on 2012-04-04
15
325 Views
Last Modified: 2012-04-05
Need a parameter for No of weeks of schedule.

define d_first_day_week  date
= date(7- mod(days(sysdate) , 7)) + (days(sysdate)) - 7)

define d_weeks  date =
date(days(d_first_day_week) + nconvert(getsys(parameter_name)) * 7)

Need help inh translating this to oracle
so that I can use in the query

select  a.department_id,a.payroll_id,
        a.schedule_date,a.start_time,a.start_ampm,
        a.end_time,a.end_ampm,a.total_hours,
        a.short_description,s.site_name,s.city_name
from off_duty a, sites s,blood_drives bd
where a.site_code = s.site_code(+)
and a.site_code = bd.site_code(+)
and a.schedule_date = bd.drive_date(+)      
and a.department_id = 5
and a.schedule_date = parameter_name(no of weeks of schedule)
0
Comment
Question by:anumoses
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 2
15 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 37807604
you have declarations that use non-Oracle functions  - what do those functions do?

you have a query that uses another non-Oracle function - what does that function do?


there is no input, nor expected output
what is the question?
0
 
LVL 6

Author Comment

by:anumoses
ID: 37808301
The logic is I have to use 1 to 5 weeks.
The first day has to be a sunday. so 1 - sunday to saturday. (april 8th to april 14th  2012)
2 weeks -( april 8th to april 21st 2012).
So when the user puts 1 then I have to to take the schedule_date = to that parameter. Internally convert 1 to sunday to saturday
0
 
LVL 6

Author Comment

by:anumoses
ID: 37808309
No I take back. I will post very soon what is required
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 74

Expert Comment

by:sdstuber
ID: 37808324
>>> convert 1 to sunday

I understand that part,    

DECODE(n,
              1, 'Sunday',
              2, 'Monday',
              3, 'Tuesday',
              4, 'Wednesday',
              5, 'Thursday',
              6, 'Friday',
              7, 'Saturday'
             )



the rest I still don't know what you want to do.  Please provide sample data and expected output.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37808326
sorry, didn't see the second post
0
 
LVL 6

Author Comment

by:anumoses
ID: 37808337
OK. here it is. If I am running the report today, then today is 4th april 2012. So I have to go back to sunday ie is the 1st april. Then from that sunday add no of weeks schedule, (1-5)
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37808344
Beginning of the week is:  trunc(sysdate, 'DAY')

>>add no of weeks schedule, (1-5)

Are you then wanting to addd 'weeks' to that?  if so, sumple math.
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 37808353
this will give you Sunday of the current week

trunc(sysdate,'d')


this will give you Sunday of the current week plus one week

trunc(sysdate,'d') + 7

this will give you Sunday of the current week plus two weeks

trunc(sysdate,'d') + 14

this will give you Sunday of the current week plus five weeks

trunc(sysdate,'d') + 35


if you have number of weeks as a number parameter "n",  try this...

trunc(sysdate,'d') + (7 * n)
0
 
LVL 6

Author Comment

by:anumoses
ID: 37808355
select trunc(sysdate, 'DAY') from dual

TRUNC(SYSDATE,'DAY')
4/1/2012

So its sunday. Then I have to add 1(1 week) 2 (2 weeks) to 4/1/2012 and that has to be used in the query

select  a.department_id,a.payroll_id,
        a.schedule_date,a.start_time,a.start_ampm,
        a.end_time,a.end_ampm,a.total_hours,
        a.short_description,s.site_name,s.city_name
from off_duty a, sites s,blood_drives bd
where a.site_code = s.site_code(+)
and a.site_code = bd.site_code(+)
and a.schedule_date = bd.drive_date(+)      
and a.department_id = 5
and a.schedule_date = ????????
0
 
LVL 6

Author Comment

by:anumoses
ID: 37808358
OK got you. Will try
0
 
LVL 6

Author Comment

by:anumoses
ID: 37808514
select trunc(sysdate,'d'), trunc(sysdate,'d') + (7 * 1) from dual

TRUNC(SYSDATE,'D')      TRUNC(SYSDATE,'D')+(7*1)
4/1/2012                             4/8/2012

4/1/2012 is correct. But I have to get 4/7/2012 ( saturday)
0
 
LVL 6

Author Comment

by:anumoses
ID: 37808519
select trunc(sysdate,'d'), trunc(sysdate,'d') + (6 * 1) from dual

This is what I did.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37809108
>>This is what I did.

Does that get you what you want?

If so, don't forget to close out the question.

If not, what else do you need?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37809114
I suggest doing this...

select trunc(sysdate,'d'), trunc(sysdate,'d') + -1 + (7 * 1) from dual

if you try multiplying 6 for multiples of weeks you'll get the wrong result.
0
 
LVL 6

Author Closing Comment

by:anumoses
ID: 37810700
thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question