Solved

oracle date query

Posted on 2012-04-04
15
319 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bulk insert into global temporary table 2 63
oracle query help 18 99
sql for Oracle views 8 49
Processing of multiple cursor 6 35
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

895 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

11 Experts available now in Live!

Get 1:1 Help Now