Select field conditionally from mysql - mysql

Is it possible to use Select with Case to select from one of several fields depending on which of them is null or not?
I basically want to return a value for all records with logic that says
Return $Value from FIeldA if not null
else from FieldB if not NULL
else from FieldC if not Null
else '0'
I've used Case/When/Then to compare values from a specific field but not as a way of comparing values between fields and not sure if this is possible.

Short answer, use COALESCE:
SELECT COALESCE(FieldA, FieldB, FieldC, 0) AS FieldName
FROM tableName;
It will give you the first non nullable value from the three fields FieldA, FieldB, FieldC. If all are null, then it will return 0. Which what you are trying to do.
Long answer, use CASE expression.

if you want to check multiple filed and if null then return same result 0 then use COALESCE function . this is simple code
SELECT COALESCE(filed1, filed2, filed3, 0) as output from table;
for more information
https://www.w3schools.com/sql/func_sqlserver_coalesce.asp
extra option if you want to select filed using condition then use case. this is demo code
SELECT CASE 1 WHEN 1 THEN 'this is case one'
WHEN 2 THEN 'this is case two'
ELSE 'this is not in the case'
END as 'how to execute case statement';
for more information
https://dev.mysql.com/doc/refman/5.7/en/case.html
http://www.mysqltutorial.org/mysql-case-function/

Related

MySQL return case value

I'm looking for a way to return the value that was "case"-ed on.
e.g. what will return me the md5 result without recomputing it at the then statement
SELECT
CASE md5(col1,col2,col3,...,coln)
WHEN MD5('') then NULL
else ???
end
If you do not want to repeat a calculation multiple times in the select list, then you need to push it into a subquery and reference the field alias set in the subquery:
SELECT
CASE t1.calc
WHEN MD5('') then NULL
else t1.calc
end as md_5
FROM
(select md5(col1,col2,col3,...,coln) as calc from table) t1

MySQL: How can you update all NULL/blank values in return within the query?

Is there a way to update any return values in MySQL that come back as either NULL or blank to "Unknown" or any value? My NULLs and blanks exist because I'm joining quite a few tables together and sometimes records exist but are blank and sometimes records don't exist in the other tables at all.
I would prefer not to update the original table because I don't save my result each time I run the query -- I just copy and paste the return into Excel before I send out the report.
Basically, it's just getting annoying sending this out multiple times a day and after pasting into Excel hitting Ctrl+F and replacing anywhere that says "NULL" with "Unknown" and then doing it again to replace any blank cell with "Unknown."
After looking around, I found IFNULL which obviously works if it's NULL but not blank -- but (1) it would be great to not have to wrap every part of my SELECT statement with IFNULLs if possible and (2) use something to encompass the blanks as well.
Just hoping there's something that I could put at the end of the query or something that I can't find. But it might just not exist within the way I'm doing it. I don't think this question needs code or schema because of the general-ness of it, but I'm certainly happy to get more specific if it helps. Thanks!
You could wrap all of the columns that could be null in a case statement that checks if any of them are null or blank before returning them.
For example:
SELECT
CASE WHEN firsttable.column1 IS NULL or firsttable.column1 = '' THEN 'Unknown' ELSE firsttable.column1 END AS firsttable.column1
CASE WHEN firsttable.column2 IS NULL or firsttable.column2 = '' THEN 'Unknown' ELSE firsttable.column2 END AS firsttable.column2
CASE WHEN secondtable.column1 IS NULL or secondtable.column1 = '' THEN 'Unknown' ELSE secondtable.column1 END AS firsttable.column1
CASE WHEN secondtable.column2 IS NULL or secondtable.column1 = '' THEN 'Unknown' ELSE secondtable.column2 END AS firsttable.column2
FROM
firsttable,
secondtable
WHERE
firsttable.id = secondtable.firsttableid
You can use IF in combination with COALESCE:
SET #val := '';
select IF(COALESCE(#val,'')='','Unknown',#val);
Returns 'Unknown', the same will returns for NULL. Any other value will be returned:
SET #val := 'test';
select IF(COALESCE(#val,'')='','Unknown',#val);
Returns 'test'.
Trick is in COALESCE - it returns first non-NULL value, so COALESCE(NULL,'') will return '' and COALESCE('','') will return '' too. You can replace variable #val with any column from table or view etc.

When statment to replace Null data

money
20.00
17.87
5.00
NULL
3.00
I want the null entries turned into zeroes. The money column is for money spent. Ive tried the following and it didnt like changing NUll to a dollar value.
select case money when 'NULL' then 0 end
from mytable
select coalesce(money, 0) from mytable
Coalesce will take the first non-null value in the list.
Try:
SELECT IsNull(money, 0)
FROM mytable
EDIT
If you want to replace the values in the table:
UPDATE mytable
SET money = 0
WHERE money Is Null
While I agree with the other two answers, I wasnted to explain why what you did was not right.
select case money when 'NULL' then 0 end
from mytable
Here you are treating NULL as a string value. But NULL is a condition not a value. It means there is no known value. If you inserted the actual word 'null' into the table in that field, then your code would have worked.
When you want to test for a NULL condition you use:
WHERE field1 IS NULL
or you change the value using coalesce or ISNULL. If you need to test for NULL as part of a case statement, then you would do it like this:
CASE WHEN field1 IS NULL THEN 0 ELSE T1 END
For simple cases like yours, COALESCE or ISNULL is best. But at times you may need to do a very complicated CASE and then it is handy to know how to do it.

COALESCE() for blank (but not null) fields

I have two fields that I'm comparing with MySQL's function COALESCE(). For example, COALESCE(Field1, Field2). The problem is, Field1 is sometimes blank but not null; since it's not null COALESCE() selects Field1, even though its blank. In that case, I need it to select Field2.
I know I can write a if-then-else (CASE) statement in the query to check for this, but is there a nice simple function like COALESCE() for blank-but-not-null fields?
SELECT IFNULL(NULLIF(Field1,''),Field2)
NULLIF returns a NULL if Field1 is blank, while IFNULL returns Field1 if it's not blank or NULL and Field2 otherwise.
I know I'm late to the party here, but there is a way to do this while still using COALESCE(). This would then work if your value was NULL or ''.
Select COALESCE(NULLIF(Field1,''), Field2)
You can use a CASE expression:
CASE WHEN Field1 <> '' THEN Field1 ELSE Field2 END
Use CASE, for comparing both NULL and Blank.
SELECT CASE
WHEN Field1 IS NULL
OR LEN(LTRIM(RTRIM(Field1))) < 1
THEN Field2
ELSE Field1
END;
For comparing blank, do not use '', use LEN, LTRIM, RTRIM. Sometimes, blank may be with more than one spaces.

MySQL GROUP BY NULL and EMPTY

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.