Solved

Case statement returning unexpected result

Posted on 2015-01-07
8
206 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
 
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
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 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

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.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now