Query case sensitive in SQL database without setting database to case sensitive - sql

I have a column IsSeeded with values 'N' and 'NULL' as one of the possibilities
I want to select all records that don't have a 'N' in their field.
when I query
select * from database.file where IsSeeded !='N';
it also doesn't return the files with values 'NULL'. How can I allow 'NULL' values without having to change my databasepreferences to case sensitive?

You have to use the IS operator when comparing to null values instead of the normal compare operators (!=/=/<>/...)
select * from database.file
where IsSeeded IS NULL;
Otherwise the result will be unknown for null entries and the condition is false.

Related

How to remove 'NULL' from results of queries SQL Server 2008

I have a table with 59 columns and over 17K rows. Lots of the rows have NULL in some of the columns.
I want to remove the NULL so that queries return a blank ('') rather than a NULL.
Can I run some update function that replaces all NULL with '' ?
Using SQL Server 2008R2 Management Studio.
UPDATE my_table
SET column_1 = REPLACE (column_1,NULL,'')
But that would take forever to do it to all 59 columns!
What's the trick, team?
Use the SQL standard COALESCE:
UPDATE my_table
SET column_1 = COALESCE(column_1,'')
, column_2 = COALESCE(column_2,'')
...
WHERE column_1 IS NULL OR
column_2 IS NULL OR
... -- avoid empty updates
;
Then use ALTER TABLE ... to add NOT NULL constraints to all columns that shall not have NULL to prohibit re-introducing NULL values.
Don't use ISNULL, which basically is a duplication of the standard COALESCE in some RDBMS - and not available in others. (Well, there are subtle differences, read the manual for details or even more detail here.)
Of course, the empty string ('') is only valid for string types. Not for number types, for instance.
Use isnull function. Returns specified value if null is found in column
Select isnull(col1,'')
Try this one:
UPDATE yourtable SET column1 = ' ' WHERE column1 = NULL

Selecting rows which are not null in 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

is there any difference between the queries

select field from table where field = 'value'
select field from table where field in ('value')
The reason I'm asking is that the second version allow me to use the same syntax for null values, while in the first version I need to change the condition to 'where field is null'...
When you are comparing a field to a null like field_name=NULL you are comparing to a known data type from a field say varchar to not only an unknown value but also an unknown data type as well, that is, for NULL values. When comparison like field_name=NULL again implies therefore a checking of data type for both and thus the two could not be compared even if the value of the field is actually NULL thus it will always result to false. However, using the IS NULL you are only comparing for the value itself without the implied comparison for data type thus it could result either to false or true depending on the actual value of the field.
See reference here regarding the issue of NULL in computer science and here in relation to the similarity to your question.
Now, for the IN clause (i.e. IN(NULL)) I don't know what RDBMS you are using because when I tried it with MS SQL and MySQL it results to nothing.
See MS SQL example and MySQL example.
There is no difference in your example. The second, slightly longer, query is not usually used for a single value, it is usally seen for multiple values, such as
select field from table where field in ('value1', 'value2')
yes there is difference in both this queries. In first statment you can insert only 1 value in where clause "where field = 'value'" but in second statement in where field you can insert many values using IN clause "where field in (value1,value2..)"
Examples:
1) select field from table where field ='value1';
2) select field from table where field in ('value1', 'value2')
To check null values
SELECT field
FROM tbl_name
WHERE
(field IN ('value1', 'value2', 'value3') OR field IS NULL)

Why does check against existing data not cover NULL in SQL Server 2008

This might be the situation in other databases as well but when you make the following query
SELECT * FROM MyTbl WHERE MyColumn != 'Foo'
then any record where MyColumn is, say, 'Bar' is fetched but not where MyColumn is NULL. I assume this is expected behavior and that there is a reason behind it and I'd like to know why.
Is NULL considered to be equal to 'Foo' or is it just not expected to be part of the condition because the condition (NULL != 'Foo') seems to be true.
In DB logic, NULL means that there is simply no defined data in this field. It's considered neither equal or different to anything. You have to filter on it explicitly if you want to fetch the relevant lines :
SELECT * FROM MyTbl WHERE MyColumn != 'Foo' OR MyColumn IS NULL
See Wikipedia.
In SQL Server works three-state logic, which mean that NULL = NULL UNKNOWN which treats as FALSE
In all relational databases, the null value is not equal to anything, including itself.
You would not be able to find the rows with null values for MyColumn even if your query was "select * from MyTbl where MyColumn = null". The only way to get them would be "select * from MyTble where MyColumn is null"
For a detailed explanation, see http://en.wikipedia.org/wiki/Null_(SQL)
Yep, as Tim commented, NULL values are unknown. SQL Server isn't going to make an assumption about a NULL value in order to make a match. You can check out more info about how SQL Server processes NULLs.

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.