How do you replace a NULL value in the select with an empty string?
It doesn’t look very professional to output “NULL” values.
This is very unusual and based on my syntax I would expect it to work.
I’m hoping for an explanation why it doesn’t.
select CASE prereq WHEN (prereq IS NULL) THEN " " ELSE prereq end from test;
Example of what the original table looks like, what I want, and what actually prints:
original wanted what actually prints -------- ------ --------------------- value1 value1 NULL NULL value2 value2 NULL NULL
As you can see it does the opposite of what I want, hence I tried flipping the IS NULL to IS NOT NULL and of course that didn’t fix it. I also tried swapping the position of when case, which did not work.
It seems the 3 solutions given below all do the task.
select if(prereq IS NULL ," ",prereq ) from test select IFNULL(prereq,"") from test select coalesce(prereq, '') from test
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
If you really must output every values including the NULL ones:
select IFNULL(prereq,"") from test
SELECT COALESCE(prereq, '') FROM test
Coalesce will return the first non-null argument passed to it from left to right. If all arguemnts are null, it’ll return null, but we’re forcing an empty string there, so no null values will be returned.
Also note that the COALESCE operator is supported in standard SQL. This is not the case of IFNULL. So it is a good practice to get use the former. Additionally, bear in mind that COALESCE supports more than 2 parameters and it will iterate over them until a non-null coincidence is found.
Try below ;
select if(prereq IS NULL ," ",prereq ) from test
Some of these built-in functions should work:
Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
Tests whether a value is NULL.
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.
select IFNULL(`prereq`,'') as ColumnName FROM test
this query is selecting “prereq” values and if any one of the values are null it show an empty string as you like
So, it shows all values but the NULL ones are showns in blank
The original form is nearly perfect, you just have to omit prereq after CASE:
SELECT CASE WHEN prereq IS NULL THEN ' ' ELSE prereq END AS prereq FROM test;
COALESCE. It returns the first non-NULL value.
SELECT COALESCE(`prereq`, ' ') FROM `test`
Try this, this should also get rid of those empty lines also:
SELECT prereq FROM test WHERE prereq IS NOT NULL;
UPDATE your_table set your_field="" where your_field is null