Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 NULL value in SQL Server

Author  Topic 

govindts
Starting Member

33 Posts

Posted - 2009-02-13 : 11:15:34
I have test table as below.

create table test(no varchar(10))

insert into test values('')
insert into test values(null)
insert into test values('test')

I inserted above three records. I need to know how null value is interpreting in sqlserver..

select * from test ==> output 3 records
select * from test where no is not null ==> output 2 records
select * from test where no <> '' ==> output 1 record.

In oracle, the above last two query is giving only one records.
But in sqlserver, it is giving in different way... So '' is not equal to NULL in sqlserver???? Please advise...

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 11:19:03
'' is not equal to NULL. in fact NULL is not stored as a value. it simply represents an undefined value.Under default settings, >,<,= ... wont work with NULL. you need to use IS NULL,IS NOT NULL,... for comparisons

'' on other hand is stored as a value
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2009-02-13 : 11:19:34
you are getting confused between a NULL value and a blank. They are not the same. So you are getting exactly what you are asking in your last two queries..

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 11:21:04
however you could force null into '' value in sql server using ISNULL() or COALESCE(). see this
select * from test
select * from test where no is not null
select * from test where no <> ''
select * from test where isnull(no,'') = ''
Go to Top of Page

govindts
Starting Member

33 Posts

Posted - 2009-02-13 : 12:08:54
Thanks for every one to respond for this thread.

In oracle, null and '' are same. Both treats as null...

I have code in sqlserver like this..

select * from temp where no <> '' and no is not null;

The same code, i need to move to oracle. In oracle, '' and NULL are same.. So i moved this code in oracle as below.

select * from test where no is not null.

Now you are saying, '' is equivalent to blank... What does it mean blank? Is it space?? I want to place equivalent logic in oracle... Any comments appreicated...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 12:35:50
you dont require
select * from temp where no <> '' and no is not null;

you can simplify this as
select * from temp where no <> ''

as use of <> operator already ignores null values (as i said earlier)
'' means no value.just an empty string
Go to Top of Page

govindts
Starting Member

33 Posts

Posted - 2009-02-13 : 12:49:16
Thank you.. It works in sqlserver.. But not in oracle..

Here is the oracle code..

SQL> create table test(no varchar2(20));

Table created.

SQL> insert into test values('');

1 row created.

SQL> insert into test values(null);

1 row created.

SQL> insert into test values('test');

1 row created.

SQL> select count(*) from test where no is not null;

COUNT(*)
----------
1

SQL> select count(*) from test where no is null;

COUNT(*)
----------
2

SQL> select count(*) from test where no <> '';

COUNT(*)
----------
0

SQL>

Finally, equivalent code in oracle is

select * from test where no is not null

Thanks..


quote:
Originally posted by visakh16

you dont require
select * from temp where no <> '' and no is not null;

you can simplify this as
select * from temp where no <> ''

as use of <> operator already ignores null values (as i said earlier)
'' means no value.just an empty string

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 12:51:23
i dont know about oracle. what i told was regarding sql server and this is sql server forum. post your oracle related queries in some oracle forums like www.dbforums.com
Go to Top of Page
   

- Advertisement -