Solved

Case statement returning unexpected result

Posted on 2015-01-07
8
216 Views
Last Modified: 2015-01-07
Gurus,

I'm using the "case" function in one of my queries and it's returning an unexpected result.

Statement:
(SELECT DISTINCT SPECIAL_CHAR(                                    
    CASE AFLFLDC                                                
      WHEN NULL                                                  
      THEN NULL                                                  
      ELSE '$'                                                  
        ||aflfldc/100                                            
    END,'fm99999.00')                                            
  FROM AFL , ART                                                      
  WHERE AFLFTYKEYI = 52                                          
  AND AFLARTKEYI   = ARTKEYI                                    
  AND aflkavkeyi   = artkavkeyi                                  
  )) AS CALCULATED_WAS;

Returns: $
Should Return: null

What am I doing wrong?
0
Comment
Question by:xbox360dp
  • 3
  • 3
  • 2
8 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40536372
What is the data type of AFLFLDC?

Try this:
(SELECT DISTINCT SPECIAL_CHAR(                                    
     CASE trim(AFLFLDC)
       WHEN NULL                                                  
       THEN NULL      
...
0
 

Author Comment

by:xbox360dp
ID: 40536377
varchar2
0
 

Author Comment

by:xbox360dp
ID: 40536380
Trim didn't work.
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.

 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40536388
Found that out when I set up a test case with a varchar2 field.

Try this:
(SELECT DISTINCT SPECIAL_CHAR(                                    
     CASE WHEN AFLFLDC IS NULL                                                  
       THEN NULL                                                  
       ELSE '$'                                                   
         ||aflfldc/100                                            
     END,'fm99999.00')                                            
   FROM AFL , ART                                                      
   WHERE AFLFTYKEYI = 52                                          
   AND AFLARTKEYI   = ARTKEYI                                     
   AND aflkavkeyi   = artkavkeyi                                  
   )) AS CALCULATED_WAS;

Open in new window

0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40536404
You could try decode;
( SELECT DISTINCT
         Special_Char ( DECODE ( Aflfldc,  NULL, NULL
                               , '$' || TO_NUMBER( Aflfldc ) / 100, 'fm99999.00' ) )
    FROM Afl, Art
   WHERE Aflftykeyi = 52 
     AND Aflartkeyi = Artkeyi 
     AND Aflkavkeyi = Artkavkeyi 
)) AS CALCULATED_WAS;

Open in new window

0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40536420
If you want alternatives, NVL2:

 (SELECT DISTINCT SPECIAL_CHAR(                                    
    NVL2(AFLFLDC,'$'||aflfldc/100,null)'fm99999.00')                                            
  FROM AFL , ART                                                      
  WHERE AFLFTYKEYI = 52                                          
  AND AFLARTKEYI   = ARTKEYI                                    
  AND aflkavkeyi   = artkavkeyi                                  
  )) AS CALCULATED_WAS;

Open in new window

0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40536430
Cool!
0
 

Author Closing Comment

by:xbox360dp
ID: 40536491
Thank you!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Via a live example, show how to take different types of Oracle backups using RMAN.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

856 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