Sql with empty string, same eligibility, different results [duplicate] - sql

This question already has answers here:
empty string in oracle
(4 answers)
Closed 1 year ago.
I've read many things around how Oracle handles null or empty string.
Let's say I have a table with these datas :
id
myfield
id1
mydata1
id2
mydata2
select * from mytable where myfield <> ' ';
select * from mytable where myfield is not null;
select * from mytable where myfield <> '' ;
When executing the first two query, I get some results. Why not with the third one ?, knowing that : it's a NOT clause, and even though my empty string was reinterpreted as null, it would make it as a is not null clause (making the 2nd and third quite the same), making it eligible ?

Oracle is weird in terms of handling nulls. It was designed, developed, and tested in the 70s, well before the SQL Standard was officially established. Oracle is a great engine but it has quirks; this is one of them.
The expression:
where myfield <> ''
gets automatically rephrased by the Oracle engine as:
where myfield <> null
And, since no value can satisfy this predicate, no rows are returned.
See example at db<>fiddle.

Related

PostgreSQL NULL value cannot be found [duplicate]

This question already has answers here:
postgresql NOT ILIKE clause does not include null string values
(2 answers)
Closed 7 months ago.
select * from test;
select * from test where name not in ('amtf');
Why?
As others have said, the problem here is, that you're comparing against a null value, so it returns nothing, because it considers it as false, and I'll go even further that even if you say where name <> 'admf' it wont work, and even if you add more rows it will ignore the null row, and it's not just in PostgreSQL, it doesn't work in SQL-Server or MySQL either.
As you can see in these db<>fiddles SQL-Server, MySQL, and PostgreSQL.
And the reason why it doesn't work is, because you're saying name should not equal a specific value. First name needs to be a value it should not be equal to a value, but when name is null it doesn't have a value, and even more for a side note null itself is not equal null.
The way to solve it is to convert it to a empty string by using COALESCE(name,'') or in SQL-Server you can also use isnull(name,''), and then compare it, or you can add or name is null which will return you all rows, including null, where name <> 'some value'.
Well the condition is right and the response for that as soo .
select * from test where name not in ('amtf');
your query is saying : give me all the records that the name Column is not in ('amtf').
you have 2 column's on is amtf and the other is null.
amtf will no be brought because of the condition and the other column is null -> no name set

Handling Ignoring Empty Values in Porting SQL Data

I am in the process of porting over some data from a SQL environment to my MongoDB backend. I'm familiar with using a NULL check with your SELECT statement, like so:
SELECT * FROM clients WHERE note is not NULL ORDER BY id_number
... but in this old SQL database table I'm noticing a lot of rows where the value is not null, it's simply empty. It would be pointless to port these across. So what would it look like to prevent pulling those over -- in terms of the SELECT statement syntax?
To clarify "note" values are of type varchar. In JavaScript I would just guard against an empty string " ". Is there a way to do this with a SQL statement?
Something like that :
SELECT * FROM clients
WHERE note is not NULL
AND TRIM(note) <> ''
ORDER BY id_number;

Unable to understand query after where clause in postgresql

I have a javaFX application with postgresql as a database , I go through the code . But unable to understand sql query in where clause. here is sql query.
SELECT *
FROM gr_group
WHERE gr_parent_id = ? AND gr_id <> 0
ORDER BY gr_description
You are looking at a parametrized SQL query. The question mark ? you see in the WHERE clause will be filled with an actual parameter coming from a Java variable.
<> (Not Equal To) is an operator. It is same as != operator.
This query will select all the columns from the table gr_group where gr_id is not equal to 0 and gr_parent_id specifies a place holder which will be filled later in the code. This query also orders selected data in the ascending order of column gr_description.

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.

Skip updating SQL row with empty values?

I have a table with several columns that allow NULLs. How would I go about writing a SQL Query that will allow me to skip that column in an update if the value is "empty". Should I use a boolean flag letting the query know to update that value? I do something similar in a SELECT like this
SELECT * FROM table WHERE (#boolFlag = 1 OR col1 = #col1Val)
But trying to do that for an update has proven to be an exercise in futility. Ideally I'd be able to say skip this value if true, otherwise update over my 20ish columns. Right now I can't pass in DBNull.Value, so instead I'm being forced to insert "" which is converted into whitespace (for instance, my nvarchar(50) has 50 spaces).
I'm using a SQL Server DB/Table and a DataSet in VB.NET, constructing my queries in VS2k8 if that matters.
You could do:
update MyTable
set MyColumn = case when #MyColumnValue = '' then null else #MyColumnValue end
A short form of the above would be:
update MyTable
set MyColumn = case when #MyColumnValue <> '' then #MyColumnValue end