Solved

Informix SQL to Oracle CASE Statement

Posted on 2011-03-16
21
490 Views
Last Modified: 2012-08-14
Please help to re-write the SQL on the left side highlighted in black to look like the one on the right highlighted in blue...

TIA,
N
Informix-SQL-to-Oracle-CASE-Stat.docx
0
Comment
Question by:Nakuru1234
[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
  • 10
  • 3
  • 3
  • +1
21 Comments
 
LVL 55

Accepted Solution

by:
Huseyin KAHRAMAN earned 400 total points
ID: 35151254
maybe this
CASE
            WHEN TO_NUMBER (TO_CHAR (SYSDATE, 'q')) = 1
            THEN
               SUM(CASE
                      WHEN sfhd_loan_activity.date_of_closing >=
                              ADD_MONTHS (TRUNC (SYSDATE, 'yyyy'), -12)
                           AND sfhd_loan_activity.date_of_closing <=
                                 ADD_MONTHS (TRUNC (SYSDATE, 'yyyy'), -9) - 1
                      THEN
                         Sfhd_Loan_Activity.Loan_Amt
                      ELSE
                         0
                   END)
            ELSE
               SUM(CASE
                      WHEN Sfhd_Loan_Activity.Date_Of_Closing >=
                              TO_DATE ('0101' || Year (SYSDATE), 'mmddyyyy')
                           AND Sfhd_Loan_Activity.Date_Of_Closing <=
                                 TO_DATE ('0331' || Year (SYSDATE),
                                          'mmddyyyy')
                      THEN
                         Sfhd_Loan_Activity.Loan_Amt
                      ELSE
                         0
                   END)
         END

Open in new window

0
 
LVL 55

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN earned 400 total points
ID: 35151268
more fix...
CASE
            WHEN TO_NUMBER (TO_CHAR (SYSDATE, 'q')) = 1
            THEN
               SUM(CASE
                      WHEN sfhd_loan_activity.date_of_closing >=
                              ADD_MONTHS (TRUNC (SYSDATE, 'yyyy'), -12)
                           AND sfhd_loan_activity.date_of_closing <=
                                 ADD_MONTHS (TRUNC (SYSDATE, 'yyyy'), -9) - 1
                      THEN
                         Sfhd_Loan_Activity.Loan_Amt
                      ELSE
                         0
                   END)
            ELSE
               SUM(CASE
                      WHEN Sfhd_Loan_Activity.Date_Of_Closing >=
                              TO_DATE ('0101' || TO_CHAR (SYSDATE, 'yyyy'),
                                       'mmddyyyy')
                           AND Sfhd_Loan_Activity.Date_Of_Closing <=
                                 TO_DATE (
                                    '0331' || TO_CHAR (SYSDATE, 'yyyy'),
                                    'mmddyyyy'
                                 )
                      THEN
                         Sfhd_Loan_Activity.Loan_Amt
                      ELSE
                         0
                   END)
         END

Open in new window

0
 

Author Comment

by:Nakuru1234
ID: 35157103
I went ahead and deleted the other question...so I opened a new one...therefore please don't delete this one. There should be more experts like HainKurt...who ready and willing to help rather than ask unnecessary questions. HainKurt...I really appreciate your help so far...thx.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Nakuru1234
ID: 35157197
HainKurt:

How about the rest? TIA...

N
0
 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 35166522
here are some tips so you can work on the rest:

Informix --> Oracle

Month(Current) --> to_number(to_char(sysdate,'mm'))
Year (CURRENT) --> to_number(to_char(sysdate,'yyyy'))
MDY (m,d,y) --> to_date(m||d||y,'mmddyyyy') -- assuming m,d, y are string, m & d is in this format xx, and y is in XXXX format
MDY (m,d,y) --> to_date(m||'/'||d||'/'||y,'mm/dd/yyyy') -- if m,d,y is not 0 padded

other than these, I dont see anything else...
0
 

Author Comment

by:Nakuru1234
ID: 35180738
Yah, thats pretty much it. I'll tryt make some sense out of this and finish the rest. Thx.

N
0
 

Author Comment

by:Nakuru1234
ID: 35182687
I did the necessary changes but I am getting errors...any advice?

TIA,
N
Informix-SQL-to-Oracle-CASE-Stat.docx
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35183048
I cannot open the attachment.  What is the error message you are receiving?
0
 

Author Comment

by:Nakuru1234
ID: 35189200
I did not mean to say errors...sorry? I wanted to request that you check to make sure I did the correct changes.

TIA,
N
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35189358
>>make sure I did the correct changes.

Do you get the correct results?  We don't know your code, data, ???  You should have test cases that you can run against your code to see if it is producing the correct results.

You need to learn what the Oracle functions are doing.  Once you do that, it should be a pretty easy conversion.

That said, I don't think you converted it properly.  Again, I cannot say for sure since I cannot test against your data/system but on a quick scan...

The old code:
CASE WHEN Month(CURRENT) >= 1 AND Month(CURRENT) <= 6

Does not seem to match what you use in:
CASE WHEN TO_NUMBER (TO_CHAR (SYSDATE, 'q')) = 1 THEN


0
 

Author Comment

by:Nakuru1234
ID: 35197826
If thats the case can you advice what to do on the rest of the code. I got help to do the first two queries and they work fine. So, I need help to write the correct syntax similar to the first two queries.

TIA,
N
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35197900
Experts-Exchange is a technology question and answer site.  It really isn't set up for consulting services.

What we have been trying to tell you is, we cannot rewrite your code for you.  If you require someone to come in and port all your code, I suggest you look into hiring a consultant.

You should understand the logic in the Informix code.  HainKurt provided the mapping between the Informix calls and the Oracle counterparts.

You need to convert the logic.  The flaw I posted above was just a pretty obvious one.  There can easily be smaller ones that we cannot see.

Only you know if the code works or not.

Aside from 'rewrite all my code for me', is there any specific Oracle function you are having trouble understanding?
0
 

Author Comment

by:Nakuru1234
ID: 35198015
I understand. I've rewritten most of the code and I was only seeking your advice on writing something close to the first two queries in CASE STATEMENT logic on the Oracle side. However, I don't think you're willing to help which is the same case I experienced with you on another question. Maybe you should defer this question to another expert or Guru in Oracle...
N.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35198068
please post your code and errors.

don't put them in a docx.

Just use text, the code and errors will be in text so it's not only the most portable format, it should be the easiest too.
0
 

Author Comment

by:Nakuru1234
ID: 35198467
sdstuber - I'll definitely post my code...see below. I thought putting in a word document will make it easy. I do like your positive attitude...

Qtr 3 – Nbr Closed this Qtr
CASE WHEN Month(CURRENT) >= 1 AND Month(CURRENT) <= 9
THEN
SUM ( CASE WHEN Sfhd_Loan_Activity.Date_Of_Closing >= MDY(07,01,Year( CURRENT )-1) AND
Sfhd_Loan_Activity.Date_Of_Closing <= MDY(09,30,Year( CURRENT )-1)
THEN 1 ELSE 0 END )
ELSE
SUM ( CASE WHEN Sfhd_Loan_Activity.Date_Of_Closing >= MDY(07,01,Year( CURRENT )) AND
Sfhd_Loan_Activity.Date_Of_Closing <= MDY(09,30,Year( CURRENT ))
THEN 1 ELSE 0 END )
END

ORACLE:
      CASE
           WHEN TO_NUMBER(TO_CHAR(SYSDATE, 'q')) = 1
           THEN
               SUM(
                   CASE
                       WHEN sfhd_loan_activity.date_of_closing >=
                                ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), -12)
                            AND sfhd_loan_activity.date_of_closing <=
                                    ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), -9) - 30
                       THEN
                           1
                       ELSE
                           0
                   END)
           ELSE
               SUM(
                   CASE
                       WHEN sfhd_loan_activity.date_of_closing >= TRUNC(SYSDATE, 'yyyy')
                            AND sfhd_loan_activity.date_of_closing <=
                                    ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), 9) - 30
                       THEN
                           1
                       ELSE
                           0
                   END)
       END


Qtr 3 - Amount
CASE WHEN Month(CURRENT) >= 1 AND Month(CURRENT) <= 9
THEN
SUM ( CASE WHEN Sfhd_Loan_Activity.Date_Of_Closing >= MDY(07,01,Year( CURRENT )-1)  AND
Sfhd_Loan_Activity.Date_Of_Closing <= MDY(09,30,Year( CURRENT )-1)
THEN Sfhd_Loan_Activity.Loan_Amt  ELSE 0 END )
ELSE
SUM ( CASE WHEN Sfhd_Loan_Activity.Date_Of_Closing >= MDY(07,01,Year( CURRENT ))  AND
Sfhd_Loan_Activity.Date_Of_Closing <= MDY(09,30,Year( CURRENT ))
THEN Sfhd_Loan_Activity.Loan_Amt  ELSE 0 END )
END

ORACLE:
      CASE
           WHEN TO_NUMBER(TO_CHAR(SYSDATE, 'q')) = 1
           THEN
               SUM(
                   CASE
                       WHEN sfhd_loan_activity.date_of_closing >=
                                ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), -12)
                            AND sfhd_loan_activity.date_of_closing <=
                                    ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), -9) - 30
                       THEN
                          Sfhd_Loan_Activity.Loan_Amt  
                       ELSE
                           0
                   END)
           ELSE
               SUM(
                   CASE
                       WHEN sfhd_loan_activity.date_of_closing >= TRUNC(SYSDATE, 'yyyy')
                            AND sfhd_loan_activity.date_of_closing <=
                                    ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), 9) - 30
                       THEN
                          Sfhd_Loan_Activity.Loan_Amt  
                       ELSE
                           0
                   END)
       END


Qtr 4 - Nbr Closed this Qtr
SUM ( CASE WHEN Sfhd_Loan_Activity.Date_Of_Closing >= MDY(10,01,Year( CURRENT )-1) AND
Sfhd_Loan_Activity.Date_Of_Closing <= MDY(12,31,Year( CURRENT )-1)
THEN 1 ELSE 0 END )

ORACLE:
      CASE
           WHEN TO_NUMBER(TO_CHAR(SYSDATE, 'q')) = 1
           THEN
               SUM(
                   CASE
                       WHEN sfhd_loan_activity.date_of_closing >=
                                ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), -12)
                            AND sfhd_loan_activity.date_of_closing <=
                                    ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), -12) - 31
                       THEN
                           1
                       ELSE
                           0
                   END)
           ELSE
               SUM(
                   CASE
                       WHEN sfhd_loan_activity.date_of_closing >= TRUNC(SYSDATE, 'yyyy')
                            AND sfhd_loan_activity.date_of_closing <=
                                    ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), 12) - 31
                       THEN
                           1
                       ELSE
                           0
                   END)
       END

Qtr 4 - Amount
SUM ( CASE WHEN Sfhd_Loan_Activity.Date_Of_Closing >= MDY(10,01,Year( CURRENT )-1)  AND
Sfhd_Loan_Activity.Date_Of_Closing <= MDY(12,31,Year( CURRENT )-1)
THEN Sfhd_Loan_Activity.Loan_Amt  ELSE 0 END )

ORACLE:
      CASE
           WHEN TO_NUMBER(TO_CHAR(SYSDATE, 'q')) = 1
           THEN
               SUM(
                   CASE
                       WHEN sfhd_loan_activity.date_of_closing >=
                                ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), -12)
                            AND sfhd_loan_activity.date_of_closing <=
                                    ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), -12) - 31
                       THEN
                          Sfhd_Loan_Activity.Loan_Amt  
                       ELSE
                           0
                   END)
           ELSE
               SUM(
                   CASE
                       WHEN sfhd_loan_activity.date_of_closing >= TRUNC(SYSDATE, 'yyyy')
                            AND sfhd_loan_activity.date_of_closing <=
                                    ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), 12) - 31
                       THEN
                          Sfhd_Loan_Activity.Loan_Amt  
                       ELSE
                           0
                   END)
       END

TIA,
N
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 100 total points
ID: 35200048
CASE WHEN Month(CURRENT) >= 1 AND Month(CURRENT) <= 9


So,  if Today is somewhere between January and September  then .....

your Oracle version is

  WHEN TO_NUMBER(TO_CHAR(SYSDATE, 'q')) = 1


if Today is first quarter,  i.e.  January, February, March
and both of those are under your heading "Qtr 3"  
which I assume means 3rd quarter (July, August, September) so neither version seems to be correct


looks like you're using the right functions, but you're not using them correctly
similar problems are in your other cases

MDY(10,01,Year( CURRENT )-1)   is October 1, of last year
MDY(12,31,Year( CURRENT )-1  is December 31, of last year

I don't see anything in your Oracle code that looks like it's even trying to find October.

I do see this which looks like an attempt at December but
ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), 12) - 31

this finds January 1 of next year then subtracts 31 days,  so it will be December 1 of this year

Your question about case syntax is fine,  you're using CASE correctly near as I can tell.
The problem is the individual conditions you are checking, you're not doing date manipulations that correspond between Informix and Oracle syntax



0
 

Author Comment

by:Nakuru1234
ID: 35206778
sdstuber - I understand...
0
 

Author Closing Comment

by:Nakuru1234
ID: 35257812
I was looking for advice on how to write the query but it was hard to follow the solution. Hainkurt query was helpful though...
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35257866
why the B?

your previous response said "I understand", but your closing comment says "hard to follow"

if you needed further explanation, just ask
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

728 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