Home » SQL & PL/SQL » SQL & PL/SQL » DECODE and NVL2 in WHERE clause (2 Merged) (11g Enterprise Edition Release 11.1.0.6.0)
DECODE and NVL2 in WHERE clause (2 Merged) [message #539165] Wed, 11 January 2012 15:25 Go to next message
rkhatiwala
Messages: 178
Registered: April 2007
Senior Member
oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production
PL/SQL Release 11.1.0.6.0 - Production
"CORE 11.1.0.6.0 Production"

Hi,

I have following:
DECLARE
   l_pin                        VARCHAR2(17);
   l_event_no                   VARCHAR2(9);
   l_causal_part_no             VARCHAR2(15);
   l_main_code                  VARCHAR2(8);
   l_event_id                   WC_MAIN_EVENT.event_id%TYPE;

BEGIN  
 l_pin := '5NPET46C67H271471';
 l_event_no := '058272000'; 
 l_causal_part_no := '883013K020';
 l_main_code := null;
 
  SELECT event_id INTO l_event_id 
  FROM WC_MAIN_EVENT
   WHERE event_group_id = 4
     AND pin = l_pin
     AND event_number = l_event_no
     AND part_number = l_causal_part_no 
     AND nvl2(l_main_code,event_code,1) = nvl2(l_main_code,l_main_code, 1); 
--     AND decode(l_main_code , NULL,1,event_code) = DECODE(l_main_code, NULL, 1, l_event_code);
  dbms_output.put_line('l_event_id = '|| l_event_id);

EXCEPTION
     when others then
   dbms_output.put_line('error = '|| sqlerrm);

END ;



If I use DECODE, then it gives error ORA-01722: invalid number , but if I use NVL2, it works fine. What is the difference between these two ?

Thanks
Re: DECODE and NVL2 in WHERE clause (2 Merged) [message #539168 is a reply to message #539165] Wed, 11 January 2012 18:35 Go to previous message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
The datatype decode returns is the datatype of the first return value - null in this case. Default datatype of null is varchar.
This can be fixed by wrapping the null in to_number.
Previous Topic: Date&Time format statement
Next Topic: How to parse data and add columns in a View
Goto Forum:
  


Current Time: Sun Aug 24 20:40:35 CDT 2025