Solved

Oracle query

Posted on 2014-12-10
8
146 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 51

Expert Comment

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

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 51

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
 
LVL 31

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 51

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 48

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

744 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

15 Experts available now in Live!

Get 1:1 Help Now