MySQL在NULL上格式化字符串或返回空字符串

时间:2011-11-20 17:21:47

标签: mysql string null

我正在导出SELECT result to CSV via INTO OUTFILE。我有一系列简单的SQL字符串函数用于格式化数据。例如:CONCAT('$',FORMAT(property.price,2))。如果值为NULL,我想以其他方式返回一个空字符串,但我不确定如何同时执行这两个操作。

此外,我想知道采用TinyInt值0或1并返回“是”或“否”的最简单方法。

3 个答案:

答案 0 :(得分:1)

要根据列的值返回yesno或空字符串,您可以使用大小写:

select case column when 1 then 'Yes' else 'No' end as columalias ,
       case when stringcolumn is NULL then '' else stringcolumn end as stringcolumn
from table

select case column when 1 then 'Yes' else 'No' end as columalias ,
       IFNULL(stringcolumn,'') as stringcolumn
from table

答案 1 :(得分:1)

对于tinyint,您可以使用IF运算符

select if(tinyint_value,'yes','no')

对于第一部分,您也可以使用if运算符

select if(property.price is not null, CONCAT('$',FORMAT(property.price,2)),'')

答案 2 :(得分:0)

您可以使用coalesce()功能。它返回的第一个参数不是NULL。

SELECT COALESCE(NULL, 1);
-> 1
SELECT COALESCE(2, 1);
-> 2
相关问题