?
Solved

Pl sql query for case stmt

Posted on 2008-09-30
7
Medium Priority
?
828 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
[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
  • 4
  • 3
7 Comments
 
LVL 143

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 143

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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

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

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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1500 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

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…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

771 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