Skip to Main Content
  • Questions
  • Using Not Equal to Condition in the SQL Select Statement

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question.

Asked: September 05, 2000 - 3:38 pm UTC

Last updated: August 25, 2005 - 6:40 pm UTC

Version: 7.3

Viewed 1000+ times

You Asked

Hi Tom,
I am facing a problem - using not equal to condition in the SQL Select statement. Let me explain it in more details with the following example-

if acc_type = 'Y' then
IF l_type in ('LEASE','RENT') then
IF rev_type = 'BASE' then
IF line_amt <> 0 then
billed_days = line_quantity
END IF
END IF
END IF
END IF

Decode can be used for the above condition but how to implement the ' NOT EQUAL TO' condition in Decode statement.

Please let me know how to resolve this problem.
Thanks in advance
Vidya

and Tom said...


That is easy:

decode( line_amt, 0, 'what it should be when line_amt=0',
'what it should be when line_amt<>0' );


I believe:


decode( acc_type, 'Y',
decode( rev_type, 'BASE',
decode( line_amt, 0, NULL,
decode( l_type, 'LEASE', line_quantity,
'RENT', line_quantity,
NULL
)
),
NULL ),
NULL )


will do it -- returns NULL is the predicate is not satisfied. It says:

if ( acc_type = 'y' )
then
if ( rev_type = 'base' )
then
if ( line_amt = 0 )
then
return null;
else
if ( l_type = 'lease' ) or ( l_type = 'rent' )
then
return line_quantity;
else
return null;
end if;
end if;
else
return null;
end if;
else
return null;
end if;




Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Need some Help

A reader, August 25, 2005 - 3:49 pm UTC

Tom,

How can I keep only the records where mode_1 and mode_2 are 'T'


Insert into T
(id, date1, date2, mode_1, mode_2)
Values
('000117', TO_DATE('02/18/2005 16:47:45', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/11/2005 13:04:31', 'MM/DD/YYYY HH24:MI:SS'), 'V', 'T');
Insert into T
(id, date1, date2, mode_1, mode_2)
Values
('000117', TO_DATE('03/24/2005 16:25:12', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/24/2005 16:28:58', 'MM/DD/YYYY HH24:MI:SS'), 'T', 'V');
COMMIT;


id date1 date2 Mode_1 mode_2
000117 2/18/2005 4:47:45 PM 3/11/2005 1:04:31 PM V T
000117 3/24/2005 4:25:12 PM 3/24/2005 4:28:58 PM T V


Tom Kyte
August 25, 2005 - 6:40 pm UTC

what do you mean by "keep"

please disgard the question. Sorry

A reader, August 26, 2005 - 10:42 am UTC