1

Why doesn't the following code work in SQL

SELECT * 
FROM DATA
WHERE VALUE != NULL;
dineshdileep
  • 715
  • 2
  • 13
  • 24

6 Answers6

3

we can not Compare null value with = We Special operator to compare null value in sql IS OPERATOR

SELECT * 
FROM DATA
WHERE VALUE is not NULL;

Null is not any Value.Sql consider Null as Unknown/absence of data.

Dhaval
  • 2,341
  • 1
  • 13
  • 16
2

The condition you written is not in proper format. If you want to select not null values from your table then you can use the following command

select *from table name where column name IS NOT NULL
Rojalin Sahoo
  • 1,025
  • 1
  • 7
  • 18
1

If should work if you replace "!=" with "IS NOT".

John Hodge
  • 1,645
  • 1
  • 13
  • 13
  • 1
    NULL is a special case, nothing is equal to NULL. NULLs are not picked out by this either, despite not being 1 or 2: `VALUE NOT IN (1,2)`. – mjsqu May 04 '15 at 04:23
  • They way I conceptualize it, NULL is the absence of data. Using an equality comparer between data and the absence of data it a logical non-starter. Check out this similar question for another answer: http://stackoverflow.com/questions/12853944/why-in-sql-null-cant-match-with-null – John Hodge May 04 '15 at 04:24
0

Because a NULL value indicates an absence of data, it doesn't make sense to compare something with a value to it with an equals operator.

You can make your SELECT statement work by setting ANSI_NULLS OFF

SET ANSI_NULLS OFF

before running the statement. You can get further information here:

https://msdn.microsoft.com/en-us/library/ms188048.aspx

Molloch
  • 2,261
  • 4
  • 29
  • 48
0

The code will not work in SQL because it is not possible to test for NULL values with the following operators =, <, or <>. It is not possible to compare NULL and 0 as they are not equivalent.

You can test for NULL values using the IS NULL and IS NOT NULL operators instead.

SELECT * 
FROM DATA
WHERE VALUE IS NOT NULL

http://www.w3schools.com/sql/sql_null_values.asp

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
0

Null does not contain any value. Null is neither zero nor any value. So we can't compare it using comparision operators. Instead of using '=', you should use 'IS' keyword.

    SELECT * FROM DATA WHERE VALUE IS NOT NULL;
Rahul Parit
  • 321
  • 3
  • 11