Selecting rows which are not null in sql - sql

Why doesn't the following code work in SQL
SELECT *
FROM DATA
WHERE VALUE != NULL;

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.

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

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

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

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;

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

Related

Substitute FROM ISNULL in where clause in ms sql server

We used Stored Procedure on our Queries. Some of our field got NULL value, so for us to get this value we Put the conversion of ISNULL inside the WHERE condition but per checking it affects the Process of our strodproc based on the SQL performance tool.
Ex.
SELECT * FROM tblInfo
WHERE ISNULL(fldInfo,'') <> ''
tblInfo
fldinfo
NULL
30
NULL
20
Query
SELECT * FROM tblinfo WHERE fldinfo NOT IN (30,20) - different result
SELECT * FROM tblinfo WHERE ISNULL(fldinfo,'') NOT IN (30,20) - Correct
Result
Any other Substitute process of script we can use so that we can get the value but not affecting the performance of the query.
Your approach will be non sargable. Even though you have an index it will not be used.
Right way to do this would be using IS NOT NULL condition
SELECT * FROM tblInfo
WHERE fldInfo <> ''
AND fldInfo IS NOT NULL
If you don't have a index, then create a index on fldinfo to improve the performance
Update :
Not In fails to compare the NULL values. Comparison with NULL values are unknown so it is fails to return the NULL values. Here is the correct way to do this
SELECT *
FROM tblInfo
WHERE (fldinfo NOT IN ( 30, 20 ) OR fldinfo IS NULL)
COALESCE is one option that you can try. It behaves in the same way. However the differences between performance has to be evaluated by you with some test
Some differences between ISNULL and COALESCE are outlined here:
SQL - Difference between COALESCE and ISNULL?
EDIT: Based on the tests done by multiple people and by theory, ISNULL seems to be a better option over COALESCE
Which is quicker COALESCE OR ISNULL?
No need for IIF. Simply check for NULL
WHERE fldinfo IS NOT NULL
Or, of course, use IS NULL if you want rows where this condition is met

Trying to create cleaner sql syntax

Here is what I am trying to do:
select * from table
where in ('completedDate', 'completedBy', 'cancelDate', 'cancelBy') is not null
If the four columns above are not null I need to display the records. I know I would do this with several where/and clauses but trying to learn and make my new stuff cleaner.
Is what I am trying to do above possible in a cleaner way?
If I understand correctly I guess you want to do that:
select *
from table
where completedDate is not null
and completedBy is not null
and cancelDate is not null
and cancelBy is not null
Regarding clarity of code I don't see a better way to write it, that's what I would code anyway.
EDIT: I wouldn't really do that in this case, but if this is a very common condition you can add a computed column in the table (stored or not), or create a view on top of table, and do:
select * from view where importantFieldsAreNotNull = 1
If I understand correctly, you want to return records where all four columns are not null?
The standard and (in my opinion) most readable way to do this would be:
Select
*
From
YourTable
Where
Column1 IS NOT NULL AND
Column2 IS NOT NULL AND
Column3 IS NOT NULL AND
Column4 IS NOT NULL;
To Check if all Columns are not null:
select * from table
where completedDate is not null
and completedBy is not null
and cancelDate is not null
and cancelBy is not null
You could use the COALESCE function to determine if all the column values were NULL.
The COALESCE function takes between 1 or more arguments and returns the first non-null argument. If at least one of the arguments passed into COALESCE is NOT NULL, then it will return that value, otherwise if all the arguments are NULL it returns NULL.
SELECT *
FROM TABLE
WHERE COALESCE(Column1, Column2, Column3, Column4) IS NOT NULL
Also depending on the datatypes of the columns, you may have to CAST them to the same datatype. For example, I wasn't able to use the COALECSE function on a DateTime column and a CHAR column without casting.
However, even though this would be shorter, I would not consider it "cleaner". I'd think it would be harder to read and maintain compared to having multiple ANDs in the WHERE clause.
-- Under reasonable assumption on data types:
select *
from [table]
where completedBy+cancelBy+DATENAME(yy,completedDate)+ DATENAME(yy,cancelDate)
is not null

SQL Statement to set NULL Values to initialized values

In SQL or T-SQL how can I go through a table and set fields that are NULL to empty strings if the type is textual and 0 if the fields are integer types.
Thanks for any help, I do not even know where to begin.
Well if it's just one table use a basic update query:
UPDATE MyTable
SET NumericColumn = ISNULL(NumericColumn,0),
StringColumn= ISNULL(StringColumn,'')
What you're suggesting would only work as a "one off".
If you always want those fields to be populated going forward you can set a default value on the table.
You can use IsNull function in T-SQL,
Syntax :
ISNULL ( check_expression , replacement_value )
The function coalesce( expression_1 , expression_2 , ... ) is also useful in this context.
It evaluates the specified expressions from left to right and returns the value of the first expression that evaluates to a value other than null, or null if all the specified expressions evaluate to null.
This can be useful, especially in conjunction with left joins and table rotations.

Sql identical query but showing different Result

I have written a query in Sql server 2008.
select select * from program
where program_description <> null
But,Result of above query is blank.
whereas the below query is showing me the desired result.
select * from program
where program_description is not null
Is there any difference between them...?
Yes, = and <> do not work with null.
You have to use IS NULL or IS NOT NULL
Yes there is a difference. Comparing something with null will always result in unknown.
That is why you need to compare null results with is null
yes, the difference is -- according to standard, all operation with null return null. Including <>. The only valid way to check null value is operation 'is null'.

how to filter in sql script to not include any column null

imagine there are 50 columns. I dont wan't any row that includes a null value. Are there any tricky way?
SQL 2005 server
Sorry, not really. All 50 columns have to be checked in one form or another.
Column1 IS NOT NULL AND ... AND Column50 IS NOT NULL
Of course, under these conditions why not disallow NULLs in the first place by having NOT NULL in the table definition
If it's SQL Server 2005+ you can do something like:
SELECT fields
FROM MyTable
WHERE stuff
EXCEPT -- This excludes the below results
SELECT fields
FROM MyTable
WHERE (Col1 + Col2 + Col3....) IS NULL
Adding a null to a value results in a null, so the sum of all your columns will be NULL.
This may need to change based on your data types, but adding NULL to either a char/varchar or a number will result in another NULL.
If you are looking at the values not being null, you can do this in the select statement.
SELECT ISNULL(firstname,''), ISNULL(lastname,'') FROM TABLE WHERE SOMETHING=1
This will replace nulls with string blanks. If you want another value use: ISNULL(firstname,'empty') for example. You can use anything where the word empty is.
I prefer this query
select *
from table
where column1>''
and column2>''
and (column3>'' or column3<'')
Allows sql server to use an index seek if the proper index/es exist. you would have to do the syntext for column 3 for any numeric values that could be negative.