Replace null with 0 in MySQL - mysql

I am getting NULL values in the results of an operation in MySQL.
Is there a way to convert the NULL values into the value 0?

Yes, by using COALESCE.
SELECT COALESCE(null_column, 0) AS null_column FROM whatever;
COALESCE goes through the list of values you give it, and returns the first non-null value.

I am adding this answer because no one mentioned IFNULL function
You can use IFNULL
SELECT IFNULL(column_name, 0) FROM table_name;
IFNULL will return column's value (if it has something other than NULL) otherwise second parameter passed (in this case 0).

If you messed up and have NULLs in existing table layout and want zeros, here is solution:
UPDATE `table` SET `somefield`=0 WHERE `somefield` is null

There is the COALESCE method which return the first non-null parameter, in your case :
COALESCE(field, 0)
But you can use this if you want more :
COALESCE(field1, field2, 0)

MySQL:
SELECT COALESCE(Mycolumn, 0);

you can put the 0 value into your insert input method by casting it:(int)0

Related

How to replace a column with null values in mysql

One of my columns in the table contains 'NULL' values and I would like to replace them with certain values.
I looked at this post: Replace nulls values in sql using select statement? and followed the answer.
My sql statement:
Select ifnull(`option`, 'MCQ') as `option`
from question_table
This statement returns me the columns there are already with 'MCQ', but the 'NULL' values are not replaced yet.
Need some guidance to change this.
If you want to change the data, you need an update:
update question_table
set option = 'MCQ'
where option is null;
A select statement does not change the database.
If you want to update the table use Gordon's answer, but maybe you just want to return a replacement value for NULL in the SELECT, you can use COALESCE:
SELECT COALESCE(`option`, 'MCQ') as `option`
FROM question_table
This selects MCQ for every option-value that is NULL.
Another way is using CASE:
SELECT CASE WHEN `option` IS NULL THEN 'MCQ' ELSE `option` END as `option`
FROM question_table
'NULL' is a string, NULL is a special value that means "unknown/unavailable". They are totally different things.
MySQL function IFNULL() handles NULL values (they cannot be compared using the regular comparison operators). You can use the regular comparison operators (=, <>) to work with strings, even when they are 'NULL'.
If your query produces 'NULL' values in the result set it means the values in the database are not NULL but strings ('NULL').
In this case the query you need is:
SELECT IF(`option` = 'NULL', 'MCQ', `option`) AS `option`
FROM question_table

NULL to zero with ISNULL

I have the following SQL statement:
SELECT name, SUM(growth) AS sum_buy_price, SUM(recovery) AS sum_msrp, SUM(growth)+SUM(recovery) AS total
FROM orders
WHERE id = ?
GROUP BY name
My data is coming from a CSV file that I have no control over and either 'growth' or 'recovery' can be NULL in the data, but not at the same time. I need to use ISNULL to convert the possible NULL values to zero in order for the SUM to work correctly, but I'm unsure of how/where to add the ISNULL since the SELECT is indexing another record (name).
ISNULL returns whether the argument passed is null (i.e., it is analogous to true or false). I suppose, what you need is IFNULL:
SELECT
name,
SUM(IFNULL(growth, 0)) AS sum_buy_price,
SUM(IFNULL(recovery, 0)) AS sum_msrp,
SUM(IFNULL(growth, 0))+SUM(IFNULL(recovery,0)) AS total
FROM
orders
WHERE
id = ?
GROUP BY
name
The SUM() function ignores NULL values, so you don't need to change a NULL to a 0 in order for it to work properly.
If however, all values that you're aggregating are NULL and you want to return a 0 instead of NULL you can use IFNULL() or the more common COALESCE() to show 0 as the sum instead of NULL:
SELECT COALESCE(SUM(growth),0)
ISNULL() is a valid SQL Server function, IFNULL() is the equivalent in MySQL, but all major databases make use of COALESCE() which returns the first non-NULL value in a set, ie: COALESCE(NULL,NULL,5) would return 5.
This should work for you:
SELECT name, SUM(ISNULL(growth, 0)) AS sum_buy_price,
SUM(ISNULL(recovery, 0)) AS sum_msrp,
SUM(ISNULL(growth, 0))+SUM(ISNULL(recovery,0)) AS total
FROM orders
WHERE id = ?
GROUP BY name

MySql, CONCAT and NOT IN, string values

I have such query but it doesn't select anything, but it should. So query
SELECT *
FROM _custom_access_call
WHERE CONCAT(type, name) NOT IN ('string1', 'string2', 'string3')
I manually add to table entry with null and '1sfgsg' values but it wasn't selected. Why? I need to select all entries that concat values is not in array. Help to deal with it.
If one of the values is NULL, then CONCAT() will return NULL. And NULL NOT IN (...) is always NULL. Also NULL IN (...) is always NULL. If you want to use NULL you should explicitally handle it. In this specific case, CONCAT_WS() helps, because it never returns NULL.
SELECT *
FROM _custom_access_call
WHERE CONCAT_WS('', type, name) NOT IN ('string1', 'string2', 'string3');
Also, note that this query cannot use any index.

In MySQL, can you check if a field is NULL or empty using only one comparison?

If I want to check to see if a field is NULL or empty using a MySQL query, I know I can do something like this:
column = '' OR column IS NULL
However, is there any way to check this without doing two separate comparisons?
Use COALESCE() to 'normalize' the value (convert NULL values to an empty string);
WHERE COALESCE(mycolumn, '') = ''
Read the documentation: COALESCE()
Or the other way around; convert empty strings to NULL;
WHERE NULLIF(mycolumn, '') IS NULL
Documentation: NULLIF()
Of those two, I would prefer COALESCE() as it is part of the ANSI SQL standard
You can experiment with it yourself, just do this;
SELECT
mycolumn AS orig_value,
COALESCE(mycolumn, '') AS coalesce_value,
(COALESCE(mycolumn, '') = '') AS compare_result
FROM mytable;
This will show the original value, the 'coalesce' value and the result of the comparison side by side for every row in the table
WHERE COALESCE(column, '') = ''
Another method without WHERE, try this..
Will select both Empty and NULL values
SELECT ISNULL(NULLIF(fieldname,'')) FROM tablename
Try this:
WHERE NOT(column LIKE '_%')
Without the NOT, the value of column must at least have one character and can not be NULL.
EDIT: MySQL still seems to swallow the NULL value this way. This should work better:
WHERE IFNULL(column, '') = ''

mysql NULL value in where in CLAUSE

how to deal with NULL value in mysql where in CLAUSE
i try like
SELECT * FROM mytable WHERE field IN(1,2,3,NULL)
it not working
only work like :
SELECT * FROM mytable WHERE field IN(1,2,3) OR field IS NULL
how can i get it work in WHERE IN ? it is possible ?
There is a MySQL function called COALESCE. It returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
If you for example run SELECT COALESCE(NULL, NULL, -1); you will get -1 back because it's the first non-NULL value.
So the trick here is to wrap your expression in COALESCE, and add a value as the last parameter that you also add in your IN function.
SELECT * FROM mytable WHERE COALESCE(field,-1) IN (1,2,3,-1)
It will only match if field is 1,2 or 3, or if field is NULL.
As by my understanding you want to pull every record with 1,2,3 and null value.
I don't think its possible to put null in the IN operator. Its expects values and null is well.. not a value. So You really have to put the OR with the null to get the desired result.
Maybe this information from the MySQL Reference Manual helps:
To comply with the SQL standard, IN returns NULL not only if the expression on the left hand side is NULL, but also if no match is found in the list and one of the expressions in the list is NULL.
Using UNION as a subquery in IN operator can get tableIds as a list and from that can get results with the NULL value.
eg:
SELECT * FROM
mytable
WHERE mytable.id IN(
SELECT mytable.id
FROM mytable
where mytable.field IS NULL
UNION
SELECT mytable.id
FROM mytable
WHERE mytable.field IN(1,2,3)
)
Following statement should help:
SELECT * FROM mytable WHERE COALESCE(field,0) IN (1,2,3,0)