MySql Query Replace NULL with Empty String in Sele

2019-03-11 14:08发布

How do you replace a NULL value in the select with an empty string? It doesnt look very professional to output "NULL" values.

This is very unusual and based on my syntax I would expect it to work. Hoping for an explanation why it doesnt.

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 actual 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 didnt fix it, also tried swapping the position of when case, which did not work.

Edit: It seems the 3 solutions given below all do the task. regards

select if(prereq IS NULL ," ",prereq ) from test
select IFNULL(prereq,"") from test
select coalesce(prereq, '') from test

10条回答
疯言疯语
2楼-- · 2019-03-11 14:24

Try this, this should also get rid of those empty lines also:

SELECT prereq FROM test WHERE prereq IS NOT NULL;
查看更多
放荡不羁爱自由
3楼-- · 2019-03-11 14:25

If you really must output every values including the NULL ones:

select IFNULL(prereq,"") from test
查看更多
倾城 Initia
4楼-- · 2019-03-11 14:27

Some of these built in functions should work:

Coalesce
Is Null
IfNull
查看更多
做自己的国王
5楼-- · 2019-03-11 14:35
UPDATE your_table set your_field="" where your_field is null
查看更多
登录 后发表回答