Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

NULL values in IN condition

 
   Database Help (Home) -> Oracle RSS
Next:  SSMS rendering issues and .Net Runtime 2.0 crash  
Author Message
Álvaro_G._Vicario

External


Since: Apr 11, 2008
Posts: 1021



(Msg. 1) Posted: Thu Dec 11, 2008 12:26 pm
Post subject: NULL values in IN condition
Archived from groups: comp>databases>oracle>misc (more info?)

I want to obtain all the possible values of a VARCHAR2(1) column named
FOO, sort them (either ASC or DESC) and finally fetch to two top-most
rows. NULL values must be listed on top (ASC) or bottom (DESC). So I've
written these queries:

SELECT * FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
-- Returns: NULL, 'A'

SELECT * FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO DESC NULLS LAST
) WHERE ROWNUM<=2
-- Returns: 'Y', 'Z'

So far so good. Now I'd like to nest this subquery in an IN condition:

SELECT *
FROM ANOTHER_TABLE
WHERE FOO_FK IN (.......)

However, the IN condition always filters out NULL values. Something like
FOO IN (NULL, 'A', 'B') is FALSE even when FOO is null; apparently,
because FOO=NULL is not TRUE. But I need to consider rows with NULLs.

I don't know before hand if there're NULLs in my "top 2", so...

WHERE FOO_FK IS NULL OR FOO_FK IN (.......)

is not an option.


Can you think of any trick to overcome this?


The server runs Oracle 9.2.0.1.0

-----

Just before hitting "Send" I came to this idea:

SELECT *
FROM ANOTHER_TABLE
WHERE NVL(FOO_FK, 'xxxxxxxxx') IN (
SELECT NVL(FOO, 'xxxxxxxxx') FROM (
SELECT DISTINCT FOO
FROM TABLE
ORDER BY FOO NULLS FIRST
) WHERE ROWNUM<=2
)

It works but... It looks so ugly! Confused



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--

 >> Stay informed about: NULL values in IN condition 
Back to top
Login to vote
(1 vote)
Peter Nilsson

External


Since: Feb 13, 2008
Posts: 13



(Msg. 2) Posted: Thu Dec 11, 2008 5:53 pm
Post subject: Re: NULL values in IN condition [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Álvaro G. Vicario" wrote:
> I want to obtain all the possible values of a VARCHAR2(1)
> column named FOO, sort them (either ASC or DESC) and finally
> fetch to two top-most rows. NULL values must be listed on
> top (ASC) or bottom (DESC). So I've written these queries:
>
> SELECT * FROM (
>         SELECT DISTINCT FOO
>         FROM TABLE
>         ORDER BY FOO NULLS FIRST
> ) WHERE ROWNUM<=2
> -- Returns: NULL, 'A'
>
> SELECT * FROM (
>         SELECT DISTINCT FOO
>         FROM TABLE
>         ORDER BY FOO DESC NULLS LAST
> ) WHERE ROWNUM<=2
> -- Returns: 'Y', 'Z'
>
> So far so good. Now I'd like to nest this subquery in an
> IN condition:
>
> SELECT *
> FROM ANOTHER_TABLE
> WHERE FOO_FK IN (.......)
>
> However, the IN condition always filters out NULL values.
> Something like FOO IN (NULL, 'A', 'B') is FALSE even when
> FOO is null; apparently, because FOO=NULL is not TRUE.
> But I need to consider rows with NULLs.
>
> I don't know before hand if there're NULLs in my "top 2",
> so...
>
>         WHERE FOO_FK IS NULL OR FOO_FK IN (.......)
>
> is not an option.
>
> Can you think of any trick to overcome this?
>
> The server runs Oracle 9.2.0.1.0
>
> -----
>
> Just before hitting "Send" I came to this idea:
>
> SELECT *
> FROM ANOTHER_TABLE
> WHERE NVL(FOO_FK, 'xxxxxxxxx') IN (
>         SELECT NVL(FOO, 'xxxxxxxxx') FROM (
>                 SELECT DISTINCT FOO
>                 FROM TABLE
>                 ORDER BY FOO NULLS FIRST
>         ) WHERE ROWNUM<=2
> )
>
> It works but... It looks so ugly! Confused

with
tofu as (select foo
from (select distinct foo
from table
order by foo nulls first)
where rownum<=2)
select *
from another_table t
where exists
(select 1
from tofu tf
where tf.foo = t.foo
or tf.foo is null and t.foo is null)

--
Peter

 >> Stay informed about: NULL values in IN condition 
Back to top
Login to vote
Carlos

External


Since: Feb 27, 2008
Posts: 24



(Msg. 3) Posted: Fri Dec 12, 2008 12:10 am
Post subject: Re: NULL values in IN condition [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 12 dic, 02:53, Peter Nilsson wrote:
> "Álvaro G. Vicario" wrote:
>
>
>
> > I want to obtain all the possible values of a VARCHAR2(1)
> > column named FOO, sort them (either ASC or DESC) and finally
> > fetch to two top-most rows. NULL values must be listed on
> > top (ASC) or bottom (DESC). So I've written these queries:
>
> > SELECT * FROM (
> > SELECT DISTINCT FOO
> > FROM TABLE
> > ORDER BY FOO NULLS FIRST
> > ) WHERE ROWNUM<=2
> > -- Returns: NULL, 'A'
>
> > SELECT * FROM (
> > SELECT DISTINCT FOO
> > FROM TABLE
> > ORDER BY FOO DESC NULLS LAST
> > ) WHERE ROWNUM<=2
> > -- Returns: 'Y', 'Z'
>
> > So far so good. Now I'd like to nest this subquery in an
> > IN condition:
>
> > SELECT *
> > FROM ANOTHER_TABLE
> > WHERE FOO_FK IN (.......)
>
> > However, the IN condition always filters out NULL values.
> > Something like FOO IN (NULL, 'A', 'B') is FALSE even when
> > FOO is null; apparently, because FOO=NULL is not TRUE.
> > But I need to consider rows with NULLs.
>
> > I don't know before hand if there're NULLs in my "top 2",
> > so...
>
> > WHERE FOO_FK IS NULL OR FOO_FK IN (.......)
>
> > is not an option.
>
> > Can you think of any trick to overcome this?
>
> > The server runs Oracle 9.2.0.1.0
>
> > -----
>
> > Just before hitting "Send" I came to this idea:
>
> > SELECT *
> > FROM ANOTHER_TABLE
> > WHERE NVL(FOO_FK, 'xxxxxxxxx') IN (
> > SELECT NVL(FOO, 'xxxxxxxxx') FROM (
> > SELECT DISTINCT FOO
> > FROM TABLE
> > ORDER BY FOO NULLS FIRST
> > ) WHERE ROWNUM<=2
> > )
>
> > It works but... It looks so ugly! Confused
>
> with
> tofu as (select foo
> from (select distinct foo
> from table
> order by foo nulls first)
> where rownum<=2)
> select *
> from another_table t
> where exists
> (select 1
> from tofu tf
> where tf.foo = t.foo
> or tf.foo is null and t.foo is null)
>
> --
> Peter

Why on earth could someone define a column as varchar2(1)??

Cheers.

Carlos.
 >> Stay informed about: NULL values in IN condition 
Back to top
Login to vote
Carlos

External


Since: Feb 27, 2008
Posts: 24



(Msg. 4) Posted: Fri Dec 12, 2008 3:38 am
Post subject: Re: NULL values in IN condition [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 12 dic, 11:44, Thomas Kellerer wrote:
> Álvaro G. Vicario, 12.12.2008 11:34:
>
> > Carlos escribió:
> >> Why on earth could someone define a column as varchar2(1)??
>
> > What's your suggestion to store a single character?
>
> Mine would be: CHAR(1)

Mine too! Wink

Cheers.

Carlos.
 >> Stay informed about: NULL values in IN condition 
Back to top
Login to vote
Álvaro_G._Vicario

External


Since: Apr 11, 2008
Posts: 1021



(Msg. 5) Posted: Fri Dec 12, 2008 6:26 am
Post subject: Re: NULL values in IN condition [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Carlos escribió:
> Why on earth could someone define a column as varchar2(1)??

What's your suggestion to store a single character?



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
 >> Stay informed about: NULL values in IN condition 
Back to top
Login to vote
Thomas Kellerer

External


Since: Mar 12, 2008
Posts: 15



(Msg. 6) Posted: Fri Dec 12, 2008 6:26 am
Post subject: Re: NULL values in IN condition [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Álvaro G. Vicario, 12.12.2008 11:34:
> Carlos escribió:
>> Why on earth could someone define a column as varchar2(1)??
>
> What's your suggestion to store a single character?

Mine would be: CHAR(1)
 >> Stay informed about: NULL values in IN condition 
Back to top
Login to vote
Álvaro_G._Vicario

External


Since: Apr 11, 2008
Posts: 1021



(Msg. 7) Posted: Fri Dec 12, 2008 8:25 am
Post subject: Re: NULL values in IN condition [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thomas Kellerer escribió:
> Álvaro G. Vicario, 12.12.2008 11:34:
>> Carlos escribió:
>>> Why on earth could someone define a column as varchar2(1)??
>>
>> What's your suggestion to store a single character?
>
> Mine would be: CHAR(1)

*gasp*...

I had assumed that, since it didn't have the leading "2" as in VARCHAR2
it was one of those peculiar Oracle types tagged as "do not use!" like,
well, VARCHAR. And something that didn't help is that SQL Developer
doesn't allow such type in the "Add column" dialog.

This is what happens when you code in more DBMS than you're able to learn xD



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
 >> Stay informed about: NULL values in IN condition 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Outer Join with an extra condition - I am taking a left outer join to which I would like to add a condition. create table tmp_nani_student(stid number) ; insert into tmp_nani_student values(1) ; insert into tmp_nani_student values(2) ; insert into tmp_nani_student values(3) ; insert into....

not null and indexes - Hello Does oracle create automaticly indexes on not null columns ? Thanx Michal

sum with null value - Hi! I've got problem with sql statement.. in dataset like col1 col2 a 1 b 3 c 5 d null e 2 i must to sum col2, but if 1 single value of this column is null then sum must result to null, according this i wrote..

All rows if null - Hi there. I'm new to oracle and I'd want to build a form search. I'm currently using dynamic sql to append all the where clauses that has no null values on the corresponding form text boxes but I'm searching for a more elegant and static solution. In..

Crash of OCIStmtExecute when binding to NULL - Hello everyone, we are currently migrating a software to use Oracle 10 client (OCI) instead of Oracle 9. A problem occurs that manifests itself in form of an application crash and we boiled it down to an OCI snippet which doesnt work anymore. Since...
   Database Help (Home) -> Oracle All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]