Solved

Pl sql query for case stmt

Posted on 2008-09-30
7
822 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
unable to get sorting resultset 15 73
Bash Script to Analyze Oracle Schemas 11 103
execute immediate plsql block 5 46
Character matching different date formats for dates between 6 46
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
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…
Via a live example, show how to take different types of Oracle backups using RMAN.

776 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