Solved

Oracle query

Posted on 2014-12-10
8
156 Views
Last Modified: 2014-12-13
Experts,

Can someone helpme with this query.

I would like the if statement to check for  FIELDA NE "CC" but I want to include those records where FieldA is equal nulls.
0
Comment
Question by:morinia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 56

Expert Comment

by:HainKurt
ID: 40491803
where NVL(FieldA,'') <> 'CC'
0
 
LVL 32

Expert Comment

by:awking00
ID: 40491937
Since Oracle treats an "empty string" as null, you need to change the nvl statement to something like -
where NVL(FIELDA,'XX') != 'CC'
0
 
LVL 56

Accepted Solution

by:
HainKurt earned 500 total points
ID: 40491954
or you can use something like

where ... and (FIELDA is null or FIELDA != 'CC')
0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 32

Expert Comment

by:awking00
ID: 40491960
CREATE TABLE fields
(id number
,fielda varchar2(2));

insert into fields values(1,'CC');
insert into fields values(2,'DD');
insert into fields values(3,NULL);
insert into fields values(4,'CC');
insert into fields values(5,'EE');

SQL> select * from fields
  2  where nvl(fielda,'') != 'CC';

        ID FI
---------- --
         2 DD
         5 EE

SQL> select * from fields
  2  where nvl(fielda,'XX') != 'CC';

        ID FI
---------- --
         2 DD
         3
         5 EE
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 40491998
with x as (
  select 1 id, 'AA' FIELDA from dual
  union select 2, 'BB' from dual
  union select 3, '' from dual
  union select 4, Null from dual
  union select 5, 'CC' from dual
) select * from x where (FIELDA is null or FIELDA != 'CC')

ID	FIELDA
1	AA
2	BB
3	
4	

Open in new window

0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40492452
or you can use something like

where ... and (FIELDA is null or FIELDA != 'CC')

or you may use:
where ... and LNNVL(FIELDA = 'CC')

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40493010
LNNVL()
is an interesting option, in particular take note it reverses the filter to EQUALS CC

see:

no points please
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40493423
Apart from the official docs provided above, this one is quite "funny" to read ;-)

http://blog.moreno-campos.com/2009/09/conditional-function-lnnvl-the-bullsht-detector/

This built-in function becomes very handy in situations described here. Once you get used to the "weird" style ("reverse boolean filter"), you won't miss this thing ;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
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
Suggested Courses

617 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