• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 562
  • Last Modified:

Informix SQL to Oracle CASE Statement

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
Nakuru1234
Asked:
Nakuru1234
  • 10
  • 3
  • 3
  • +1
3 Solutions
 
HainKurtSr. System AnalystCommented:
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
 
HainKurtSr. System AnalystCommented:
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
 
Nakuru1234Author Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Nakuru1234Author Commented:
HainKurt:

How about the rest? TIA...

N
0
 
HainKurtSr. System AnalystCommented:
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
 
Nakuru1234Author Commented:
Yah, thats pretty much it. I'll tryt make some sense out of this and finish the rest. Thx.

N
0
 
Nakuru1234Author Commented:
I did the necessary changes but I am getting errors...any advice?

TIA,
N
Informix-SQL-to-Oracle-CASE-Stat.docx
0
 
slightwv (䄆 Netminder) Commented:
I cannot open the attachment.  What is the error message you are receiving?
0
 
Nakuru1234Author Commented:
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
 
slightwv (䄆 Netminder) Commented:
>>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
 
Nakuru1234Author Commented:
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
 
slightwv (䄆 Netminder) Commented:
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
 
Nakuru1234Author Commented:
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
 
sdstuberCommented:
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
 
Nakuru1234Author Commented:
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
 
sdstuberCommented:
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
 
Nakuru1234Author Commented:
sdstuber - I understand...
0
 
Nakuru1234Author Commented:
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
 
sdstuberCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 10
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now