Solved

Pl sql query for case stmt

Posted on 2008-09-30
7
818 Views
Last Modified: 2013-12-07
i have query where if insert date is checking between two dates i.e date1 and date 2.i want records if date2+8 is less than or equal to date1.and if date2 is greater than one week let's say 10 days greater than date1 it should only get records for 1 week.
i need to use case statement.but i am not sure how to make it.Need assistance.


  select *
         from order_network_element_response
        where case insert_dt when
        insert_dt between to_date('date1', 'MM/DD/YYYY') and
              to_date('date2', 'MM/DD/YYYY') + 8<=to_date('date1', 'MM/DD/YYYY')
         --     get all records for those days
         when
        insert_dt between to_date('date1', 'MM/DD/YYYY') and
              to_date('date2', 'MM/DD/YYYY') + 8>to_date('date1', 'MM/DD/YYYY')
       ---       get records for one week
0
Comment
Question by:ANISHKA1
  • 4
  • 3
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22604824
what about this:
select *
  from order_network_element_response
   where insert_dt between to_date('date1', 'MM/DD/YYYY') and least ( to_date('date2', 'MM/DD/YYYY') + 8, to_date('date1', 'MM/DD/YYYY'))

Open in new window

0
 

Author Comment

by:ANISHKA1
ID: 22604871
not getting expected result.
queries goal is date 2 is less than one week return those rows but if user enters date 2 greater than one week then return only one week records from date1 to date1+8.

i think we might have to use case or if statement but not sure how to implement.

Need assistance.Thanks,
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22605108
ok, simple reversion of date1 and date2 in the second part:
select *

  from order_network_element_response

  where insert_dt between to_date('date1', 'MM/DD/YYYY') 

    and least ( to_date('date1', 'MM/DD/YYYY') + 8, to_date('date2', 'MM/DD/YYYY'))

Open in new window

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.

 

Author Comment

by:ANISHKA1
ID: 22605350
not as per required..can we do procedure and put if condition for dates ..
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22605380
>not as per required..
in how far?

>can we do procedure and put if condition for dates ..
in oracle, procedures will not be able to return data for SELECT, unless using a REF CURSOR...

so, you might want to explain a bit more about how it should work, and how you are running the query?
0
 

Author Comment

by:ANISHKA1
ID: 22605490
 


let me try to explain the requirement.user has to enter date1 and date2 .in select statement or pl/sql we have to check the date2 input that date2 should not be greater than one week value of date one for example if date1='09/01/2008' then date2='09/15/2008' is not valid input date 2 is exceeding one week value than date1.in this situation we have to return only one week records.
second condition is that if date 2 is not exceeds one week condition then return only those days record.for example date1='09/01/2008' and date2='09/06/2008 then it should return 6 days recode from date1.
So the basic requirement is that record should not exceed more than one week. Following are the colums getting from query.
         MIN,MDN,ACTIVATION_DT,ACCOUNT_ID,PROCDATETIME,ERRCODE

As per my knowledge we can use condition to check date2 and date1
like
insert_dt between date1 and date2
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22605868
so, my last suggestion will do that.

the trick is the function LEAST(), which returns the lower value of the 2 arguments...
select *

  from order_network_element_response

  where insert_dt between to_date('date1', 'MM/DD/YYYY') 

    and least ( to_date('date1', 'MM/DD/YYYY') + 8, to_date('date2', 'MM/DD/YYYY'))

Open in new window

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

Suggested Solutions

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…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
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 video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

757 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

20 Experts available now in Live!

Get 1:1 Help Now