Solved

oracle date query

Posted on 2012-04-04
15
318 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
  • 8
  • 5
  • 2
15 Comments
 
LVL 73

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
 
LVL 73

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 73

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 76

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 73

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 76

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 73

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

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 …
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now