Where with two AND - sql

I'm Trying to delete rows from database that don't have phone, chellphone, and email (all 3 together)
DELETE *
FROM TEST
WHERE (((TEST.EMAIL1) IS Null)
AND ((TEST.PHONE3) IS Null)
AND ((TEST.PHONE1) IS Null));
For some reason if I'm doing only any two (email1, phone1 or phone3) it works, but when I'm adding second 'AND' it stop working. Any advise please.

Honestly the WHERE clause looks okay in this example, but the '*' should be left out:
DELETE FROM TEST
WHERE (((TEST.EMAIL1) IS Null)
AND ((TEST.PHONE3) IS Null)
AND ((TEST.PHONE1) IS Null));
If you have trouble after you delete the asterisk, re-copy-paste it back in, and we can see if there's a problem with parentheses or something. But the above bracketing looks okay (even if not necessary).

I'm not sure what is going on with your case but I've cleaned up your statement a bit, as you shouldn't need all those () as far as I know and DELETE doesn't require the * as it should delete anything that matches your criteria. Try it out and let me know!
DELETE
FROM TEST
WHERE EMAIL1 IS Null
AND PHONE3 IS Null
AND PHONE1 IS Null

DELETE *
FROM test
WHERE (((test.EMAIL1) ="") OR ((test.EMAIL1) IS NULL))
AND (((test.PHONE1) = "") OR ((test.phone1) IS NULL))
AND (((test.PHONE3) ="") OR ((test.phone3) IS NULL));
Worked for me. Thanks to all..

With all ANDs the parenthesis is not really necessary
DELETE
FROM TEST
WHERE TEST.EMAIL1 IS Null
AND TEST.PHONE3 IS Null
AND TEST.PHONE1 IS Null;
however, ensure that the values you expect to get deleted actually contain NULL and not something like empty string or the literal value of null.
You can check what information will be deleted by changing your statement to a select statement instead:
SELECT *
FROM TEST
WHERE TEST.EMAIL1 IS Null
AND TEST.PHONE3 IS Null
AND TEST.PHONE1 IS Null;
Based on your own answer to your question you were running into empty strings, not nulls. Another way to write what you wrote and to avoid ORs would be:
SELECT *
FROM TEST
WHERE isnull(TEST.EMAIL1, '') <> ''
AND isnull(TEST.PHONE3, '') <> ''
AND isnull(TEST.PHONE1, '') <> '';
In the above we're stating that any null test.email1s we encounter, treat as an empty string then check that that values is not an empty string.
So basically - if any of those three fields are null OR empty string. Same as your answer, just another way to write it.

You likely have no rows where all three are null. Check your data.

Related

Error with syntax replacing values in the query output

I work within the SQL database, but am self taught, so there are a few holes in my knowledge. I have this query that although I would think is simple, I just cant get it right.
I have two columns, one with a 'Name', and the other 'Privacy'.
I am trying to to get a result where if the Privacy column has a numerical value (3) in it, all these rows will return a 'name suppressed' in the Name column. And all the rows with a NULL in the Privacy column are left alone.
But I don't want to update the database, just suppress the name in the query.
I have tried, replace, case, if then and a few more with no luck. Can someone please help me.
Thanks
Michele
You can go for simple CASE expression. I am assuming you only want to suppress if privacy = 3.
SELECT CASE WHEN Privacy = 3 THEN 'Name Suppressed'
ELSE Name
END AS Name, Privacy
FROM TableName
SELECT
CASE
WHEN [Privacy] IS NULL THEN [Name]
ELSE N'name suppressed'
END AS [Name],
[Privacy]
FROM
[dbo].[YourTable]

Returning varchars that are not null or empty SQL

I have a column in SQL that is varchar. I need it to return anything with a value.
Example...
select * from students where StudentID <> ''
Is this the correct way of doing it? I've tried is not null but then it returns anything that is empty as well.
Thanks
I would suggest using coalesce:
select * from students where coalesce(StudentID, '') <> ''
This will turn nulls into empty strings and disallow them. This has the added bonus of restricting empty strings as well.
A null is not equal to anything, not even another null, so a simple <> doesnt work.
select * from students where StudentID <> '' AND StudentID IS NOT NULL
You can target both white space and null.
There's something call NOT NULL
SELECT LastName,FirstName,Address FROM Persons WHERE Address IS NOT NULL
Is that helping ?
try this:
select * from students where StudentID is not null
You have to handle the case that it's not null separately because you cannot compare with null-values. They are neither equal nor unequal to any other value(incl. null). NULL means "unknown value", so any comparison with any actual value makes no sense
....
WHERE StudentID IS NOT NULL AND StudentID <> ''
You could use ISNULL(StudentID,'') <> '' (or COALESCE). But i think this is more efficient.
Try using ISNULL()
select * from student where isnull(studentid,'') <> ''

Getting All Records where Particular Column is empty in Access

I am writing a query in Access where I have to get all the records where a particular column is empty, how I can do this?
This is what I am thinking it should be, but its not working.
SELECT *
FROM TABLE
WHERE PARTICULARCOLUMN = ''
This would handle both empty strings ('') and NULL values in the column.
SELECT *
FROM TABLE
WHERE Nz(PARTICULARFIELD,'') = ''
Try...
WHERE PARTICULARFIELD Is Null
Sample from Web:
SELECT [Table1].[CustomerID], [Table2].[CustomerID]
FROM [Table1] LEFT JOIN [Table2] ON [Table1].[CustomerID] = [Table2].[CustomerID]
WHERE ((([Table 2].[CustomerID]) Is Null));
See: http://www.fabalou.com/access/Queries/isnullquery.asp
In my case, I wanted to find all records with a particular field empty. Not NULL didn't work. This did: WHERE ((Not (table.field)="").
So, to find both the empties and the nulls, just to be sure...
WHERE (((table.field)="" Or (table.field) Is Null)

isnull vs is null

I have noticed a number of queries at work and on SO are using limitations in the form:
isnull(name,'') <> ''
Is there a particular reason why people do that and not the more terse
name is not null
Is it a legacy or a performance issue?
where isnull(name,'') <> ''
is equivalent to
where name is not null and name <> ''
which in turn is equivalent to
where name <> ''
(if name IS NULL that final expression would evaluate to unknown and the row not returned)
The use of the ISNULL pattern will result in a scan and is less efficient as can be seen in the below test.
SELECT ca.[name],
[number],
[type],
[low],
[high],
[status]
INTO TestTable
FROM [master].[dbo].[spt_values]
CROSS APPLY (SELECT [name]
UNION ALL
SELECT ''
UNION ALL
SELECT NULL) ca
CREATE NONCLUSTERED INDEX IX_TestTable ON dbo.TestTable(name)
GO
SELECT name FROM TestTable WHERE isnull(name,'') <> ''
SELECT name FROM TestTable WHERE name is not null and name <> ''
/*Can be simplified to just WHERE name <> '' */
Which should give you the execution plan you need.
is not null
Will only check if the field is not null. If the field contains an empty string, then the field is no longer null.
isnull(name, '') <> name
Checks for both a null and an empty string.
isnull(name,'') <> :name is shorthand for (name is null or name <> :name) (assuming that :name never contains the empty string, thus why shorthands like this can be bad).
Performance-wise, it depends. or statements in where clauses can give extremely bad performance. However, functions on columns impair index usage. As usual: profile.
isnull(name,'') <> name
Well I can see them using this because this way if the name doesn't match or is null it returns as a failed comparison. This really means: name is null or name <> name
Where as this one name is not null just checks to see if the name is null.
They don't mean the same thing.
name is not null
This checks for records where the name field is null
isnull(name,'') <> name
This one changes the value of null fields to the empty string so they can be used in a comparision. In SQL Server (but not in Oracle I think), if a value is null and it is used to compare equlaity or inequality it will not be considered becasue null means I don't know the value and thus is not an actual value. So if you want to make sure the null records are considered when doing the comparision, you need ISNULL or COALESCE(which is the ASCII STANDARD term to use as ISNULL doen't work in all databases).
What you should be looking at is the differnece between
isnull(a.name,'') <> b.name
a.name <> b.name
then you will understand why the ISNULL is needed to get correct results.
I apparently misread your question. So let me strike my first answer and try this one:
isnull(name,'') <> ''
is a misguided shortcut for
name is not null and name <> ''
Others have pointed out the functional difference. As to the performance issue, in Postgres I've found that -- oh, I should mention that Postgres has a function "coalesce" that is the equivalent of the "isnull" found in some other SQL dialects -- but in Postgres, saying
where coalesce(foobar,'')=''
is significantly faster than
where foobar is null or foobar=''
Also, it can be awesomely dramatically faster to say
where foobar>''
over
where foobar!=''
A greater than test can use the index and thus skip over all the blanks, while a not-equal test has to do a full file read. (Assuming you have an index on the field and no other index is used in preference.)
Also if you want to make use of the index on that column, use
name is not null and name <> ''
These two queries are not the same. For example, I do not have a middle name, this is a known fact, which can be stored as
MiddleName=''
However, if we don't know someone's middle name, we can store NULL.
So, ISNULL(MiddleName, '') means "persons without known middle names".
It is to handle both the empty string and NULL. While it is good to be able to do with with one statement, isnull is proprietary syntax. I would write this using portable Standard SQL as
NULLIF(name, '') IS NOT NULL

mySQL query Where issue

Could somebody tell me what am I doing wrong here?
$query = "SELECT *
FROM routes
WHERE econ <> maxecon
ORDER BY `id`;";
Without the WHERE, it works fine so I know that's the problem.
I want to select data where the column econ is NOT equal to maxecon. Is this even possible at this stage or do I have to take it all and chop it out in a loop?
Does replacing <> with != work?
$query = "SELECT * FROM routes WHERE econ != maxecon ORDER BY `id`";
Also, you don't need to include the ending semi-colon in your sql statement.
What you've posted looks ok. Have you definitely got examples where the two values are different? ( look at the results of your query without the WHERE ).
Also, are the fields nullable? Been a long time since I've used MySQL, but on SQL Server, items are not comparable with the <> operator if one of the operands is NULL.
Try :-
SELECT * FROM routes
WHERE
( econ <> maxecon )
OR ( econ IS NULL AND maxecon IS NOT NULL )
OR ( econ IS NOT NULL AND maxecon IS NULL )
ORDER BY id;
Quoted for your implementation language, of course.
I would try != instead of <>.
Your query should be:
$query = "SELECT * FROM routes WHERE econ != maxecon ORDER BY `id`";
Of course, if econ is equal to maxecon then you will get no results, maybe you could post some sample data from the table?
Possible problems:
You may possibly need to backquote the names econ and maxecon.
You may not have any data that meets the critieria.
You may have data that meets the critieria, but only in cases where one column is NULL, and these won't be found by <>.
If the your problem is the last one, try NOT <=>, which is the negation of a special "NULL safe equals" operator in MySQL.
What programming language is this? Most I've used don't make you put a semicolon in the query itself...