Link to home
Start Free TrialLog in
Avatar of jrbledsoe001
jrbledsoe001

asked on

Convert a SQL case statement to an Oracle case statement

Hello,

I have Crystal SQL command report which uses a case statement.  The sql command works great on a MS SQL server database.  I need to run the same report on an Oracle 10g database.  

Avatar of jrbledsoe001
jrbledsoe001

ASKER

The Crystal report uses two SQL Command parameters:  RptPeriod and RptYear.  The RptPeriod parameter allows the user to select current, previous, or fiscal report period 1 through 12.  The RptYear parameter allows the user to select current, last, or enter a year in YYYY format.

Here is the code:

SELECT
COMPANY,
ACCT_PERIOD =
      CASE '{?RptPeriod}'
      WHEN 'Current Period' THEN ACCT_PERIOD
      WHEN 'Previous Period' THEN
            CASE ACCT_PERIOD
            WHEN 1 THEN 12
            ELSE (ACCT_PERIOD - 1)
            END
      ELSE CAST('{?RptPeriod}' AS INTEGER)
      END,
FISCAL_YEAR =
      CASE '{?RptYear}'
      WHEN 'Current Period Year' THEN FISCAL_YEAR
      WHEN 'Previous Period Year' THEN
            CASE ACCT_PERIOD
            WHEN 1 THEN FISCAL_YEAR - 1
            ELSE FISCAL_YEAR
            END
      ELSE CAST('{?RptYear}' AS INTEGER)
        END,
FISCAL_YEAR_BASE =
      CASE '{?RptYear}'
      WHEN 'Current Period Year' THEN FISCAL_YEAR
      WHEN 'Previous Period Year' THEN
            CASE ACCT_PERIOD
            WHEN 1 THEN FISCAL_YEAR - 1
            ELSE FISCAL_YEAR
            END
      ELSE CAST('{?RptYear}' AS INTEGER)
        END
FROM GLSYSTEM
UNION ALL
SELECT
COMPANY,
ACCT_PERIOD =
      CASE '{?RptPeriod}'
      WHEN 'Current Period' THEN ACCT_PERIOD
      WHEN 'Previous Period' THEN
            CASE ACCT_PERIOD
            WHEN 1 THEN 12
            ELSE (ACCT_PERIOD - 1)
            END
      ELSE CAST('{?RptPeriod}' AS INTEGER)
      END,
FISCAL_YEAR =
      CASE '{?RptYear}'
      WHEN 'Current Period Year' THEN FISCAL_YEAR - 1
      WHEN 'Previous Period Year' THEN
            CASE ACCT_PERIOD
            WHEN 1 THEN FISCAL_YEAR - 2
            ELSE FISCAL_YEAR - 1
            END
      ELSE CAST('{?RptYear}' AS INTEGER) - 1
        END,
FISCAL_YEAR_BASE =
      CASE '{?RptYear}'
      WHEN 'Current Period Year' THEN FISCAL_YEAR
      WHEN 'Previous Period Year' THEN
            CASE ACCT_PERIOD
            WHEN 1 THEN FISCAL_YEAR - 1
            ELSE FISCAL_YEAR
            END
      ELSE CAST('{?RptYear}' AS INTEGER)
        END
FROM GLSYSTEM;
Avatar of Raja Jegan R
In Oracle, CASE statement syntax is same as that of SQL Server..
Hence it would be more helpful if you could post your existing code so that we can change it in case if it requires any other modifications.
Oracle does not like the syntax where the field name is followed by the "=" sign; example
ACCT_PERIOD = .
- as I look into it, Oracle CASE expression is almost the same as in MSSQL. you can start with the oracle documentation on Case expression:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/expressions004.htm
Since you have RptPeriod and RptYear variables having datetime values, you need to separate month and year values from those variables using to_char function like shown below:

RptPeriod - to_char(RptPeriod, 'mm');
RptYear - to_char(RptYear, 'YYYY');
And kindly let me know the datatype of ACCT_PERIOD along with some sample values..
I think this is of datatype datetime and you need to use to_char as mentioned in my earlier comment
ASKER CERTIFIED SOLUTION
Avatar of OP_Zaharin
OP_Zaharin
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you both for the speedy responses.  I'll modify the code with your suggestions and post the results.
Thank you P_Zaharin.  Your solution was perfect
glad to help :)