In a MySQL query I am doing a GROUP BY with a text field. Due to the nature of the original data, some rows are contain empty strings for this field, and others are true null.
When grouping, how can I group empty string and null together, treating both as null?
This can be accomplished by SELECT CASE. There may be a simpler way I do not know of.
The format of SELECT CASE is
SELECT
CASE
WHEN table_name.text_field IS NULL OR table_name.text_field = ''
THEN null
ELSE table.text_field
END as new_field_name,
other_field, another_field, ...rest of query...
So you see, you can CASE together values with WHEN/THEN and default to the real value by ELSE.
Related
Say I have a query that is something like this:
select
table1.column1,
table1.column2,
...,
case when foo.id is null then 'N/A' else sum(foo.points) end as score -- same result using ifnull()
from
table1 inner join table2 on ...
left join foo on ...
group by ...
Since I do a LEFT JOIN on foo, there is a chance that there is no match. In that case, I want the score to show as 'N/A' instead of NULL or 0. But when I do the above query, I get blob for the score column for all rows.
When you have different data types in the results in the case it will return a value with the data type blob if it can't find a common type.
Try casting the sum to a char:
case when foo.id is null then 'N/A' else cast(sum(foo.points) as char) end as score
If you are grouping, you should really put your sum around the case, like:
sum(case when foo.id is null then 0 else foo.points)
..as otherwise you are summing just the row value (meaning only one value).
Also, a column should only have one data type, so either number or text, which is why you might be seeing this issue since you are trying to display either a number or text in the same column.
If you really want N/A, you can try converting the number to text, and then using the coalesce function to handle nulls, however I would need to see your data to say the best way to write your above query. If you can create an SQL fiddle, I would be more than happy to take a look :)
SUM up the the results of the CASE..WHEN.
SUM(CASE WHEN foo.id IS NULL THEN NULL ELSE foo.points) END AS score
You can display the default value in the frontend application (n/a) when the score field is null (or equals to 0).
The score will be NULL when there all rows has null in foo.id.
I have my Mysql Query as :
select RecordID,ID,Name,Description,RecordDateTimeUTC,Location from
eemployee where RecordID like ? and(isNull(RecordDateTimeUTC) OR RecordDateTimeUTC= CASE WHEN COALESCE(?,'') = '' THEN RecordDateTimeUTC ELSE ? END)........
Now my query There are lott of fields(? --> this is where i am putting my data for filter perpose) upon which user can query so i am using coalesce but the disadvantage of using coalesce is it doesn't give me null values when i want to get all data present in employee table , so to get null values i have used isNull function.
Now i am testing the above query for two use cases
1-->User can pass percentage in RecordID:-In this case he should get all data either null as well as not null values :
select RecordID,ID,Name,Description,RecordDateTimeUTC,Location from
eemployee where RecordID like '%'
and(isNull(RecordDateTimeUTC) OR RecordDateTimeUTC= CASE WHEN COALESCE('','') = '' THEN RecordDateTimeUTC ELSE '' END)
2-->User can filter data based on RecordDateTimeUTC:- If user passes this parameter then he should get all not null data satisfying the above filteration.:-
select RecordID,ID,Name,Description,RecordDateTimeUTC,Location from
eemployee where RecordID like '%' and(isNull(RecordDateTimeUTC) OR RecordDateTimeUTC= CASE WHEN COALESCE('2012-07-09 11:11:00','') = '' THEN RecordDateTimeUTC ELSE '2012-07-09 11:11:00' END)
For the first use case it is working fine but for the second use case it is giving me filtered data as well as null Data
So what should be my query so that both my use cases are supported by the above single query.
I am using MYSQl Query Browser to test my query
Thanks in advance
It looks like you're trying to make parameters optional while using a single static SQL statement. In that case I would imagine you mean something like:
SELECT RecordID,ID,Name,Description,RecordDateTimeUTC,Location
FROM eemployee
WHERE (#RecordId IS NULL OR RecordID LIKE #RecordId)
AND (#RecordDateTimeUTC IS NULL OR RecordDateTimeUTC = #RecordDateTimeUTC)
If the parameter value is NULL, it will be omitted completely from the filter, otherwise the passed value will be used.
Note you can't use ? here because of the need to re-use the parameter - unless you want to specify each parameter twice, that is. Specifying a name makes the query a lot more readable IMHO.
I'd like to limit my query to show only rows where a certain field is not empty. I found this thread where someone posed the same question and was told to use IS NOT NULL. I tried that, but I'm still getting rows where the field is empty.
What is the correct way to do this? Is Null the same thing as Empty in SQL/MySQL?
My query, if you're interested is:
SELECT * FROM records WHERE (party_zip='49080' OR party_zip='49078' OR party_zip='49284' ) AND partyfn IS NOT NULL
I got it by using AND (partyfn IS NOT NULL AND partyfn != '')
When comparing a NULL value, the result in most cases becomes NULL and therefor haves the same result as 0 (the FALSE value in MySQL) inside WHERE and HAVING.
In your given example, you don't need to include IS NOT NULL. Instead simply use party_zip IN ('49080', '49078', '49284'). NULL can't be 49080, 49078, 49284 or any other number or string.
What you do need to think about though, is when checking for empty values. !party_zip won't return TRUE/1 if the value is NULL. Instead use OR columns IS NULL or !COALESCE(party_zip, 0)
I'm having a problem where when I try to select the rows that have a NULL for a certain column, it returns an empty set. However, when I look at the table in phpMyAdmin, it says null for most of the rows.
My query looks something like this:
SELECT pid FROM planets WHERE userid = NULL
Empty set every time.
A lot of places said to make sure it's not stored as "NULL" or "null" instead of an actual value, and one said to try looking for just a space (userid = ' ') but none of these have worked. There was a suggestion to not use MyISAM and use innoDB because MyISAM has trouble storing null. I switched the table to innoDB but now I feel like the problem may be that it still isn't actually null because of the way it might convert it. I'd like to do this without having to recreate the table as innoDB or anything else, but if I have to, I can certainly try that.
SQL NULL's special, and you have to do WHERE field IS NULL, as NULL cannot be equal to anything,
including itself (ie: NULL = NULL is always false).
See Rule 3 https://en.wikipedia.org/wiki/Codd%27s_12_rules
SELECT pid FROM planets WHERE userid IS NULL
As all are given answers I want to add little more. I had also faced the same issue.
Why did your query fail? You have,
SELECT pid FROM planets WHERE userid = NULL;
This will not give you the expected result, because from mysql doc
In SQL, the NULL value is never true in comparison to any other value, even NULL. An expression that contains NULL always produces a NULL value unless otherwise indicated in the documentation for the operators and functions involved in the expression.
Emphasis mine.
To search for column values that are NULL, you cannot use an expr = NULL test. The following statement returns no rows, because expr = NULL is never true for any expression
Solution
SELECT pid FROM planets WHERE userid IS NULL;
To test for NULL, use the IS NULL and IS NOT NULL operators.
operator IS NULL tests whether a value is NULL.
operator IS NOT NULL tests whether a value is not NULL.
MySQL comparison operators
There's also a <=> operator:
SELECT pid FROM planets WHERE userid <=> NULL
Would work. The nice thing is that <=> can also be used with non-NULL values:
SELECT NULL <=> NULL yields 1.
SELECT 42 <=> 42 yields 1 as well.
See here: https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to
Info from http://w3schools.com/sql/sql_null_values.asp:
1) NULL values represent missing unknown data.
2) By default, a table column can hold NULL values.
3) NULL values are treated differently from other values
4) It is not possible to compare NULL and 0; they are not equivalent.
5) It is not possible to test for NULL values with comparison
operators, such as =, <, or <>.
6) We will have to use the IS NULL and IS NOT NULL operators instead
So in case of your problem:
SELECT pid FROM planets WHERE userid IS NULL
Had the same issue where query:
SELECT * FROM 'column' WHERE 'column' IS NULL;
returned no values.
Seems to be an issue with MyISAM and the same query on the data in InnoDB returned expected results.
Went with:
SELECT * FROM 'column' WHERE 'column' = ' ';
Returned all expected results.
SELECT pid FROM planets WHERE userid is null;
I had the same issue when converting databases from Access to MySQL (using vb.net to communicate with the database).
I needed to assess if a field (field type varchar(1)) was null.
This statement worked for my scenario:
SELECT * FROM [table name] WHERE [field name] = ''
I have a table with name,age and address.I have totally five rows of data in the table.For some rows the age is left null.I have to display all the data where age is not null.
select * from sample_table where (age !=null);
But no output is displayed and it doesn't give an error also.Please explain this.Thanks.
With NULL you have to use IS or IS NOT. The following query should work:
SELECT * FROM sample_table WHERE (age IS NOT NULL)
The explanation from MySQL
The concept of the NULL value is a common source of confusion for
newcomers to SQL, who often think that
NULL is the same thing as an empty
string ''. This is not the case.
In SQL, the NULL value is never true in comparison to any other value,
even NULL. An expression that contains
NULL always produces a NULL value
unless otherwise indicated in the
documentation for the operators and
If you want to search for column values that are NULL, you cannot use
an expr = NULL test.
To look for NULL values, you must use the IS NULL test.
You have to use IS NULL or IS NOT NULL.
You can not compare directly against NULL because it is not value (no pedants please!)
NULL on Wikipedia
MySQL, Sybase, SQL Server... it's all the same