MySQL只在null时才将double转换为varchar

时间:2012-09-25 11:30:39

标签: mysql casting

我正在尝试从数据库中获取DOUBLE类型值以用于报告。如果此值为NULL,则报告应说明"N/A"

我只是在使用sql时遇到了一些困难:

SELECT CAST(IFNULL(billablerate, 'N/A'), CHAR(5)) AS billablerate FROM rates

有些东西告诉我这些功能不能像这样组合,但我不知道如何将它们两者都用到。

任何帮助都会很棒!

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT IFNULL(CAST(billablerate AS char (5)), 'N/A') AS billablerate FROM rates

适用于:

SELECT IFNULL(CAST(null AS char (5)), 'N/A') AS exp; // N/A
SELECT IFNULL(CAST(10 AS char (5)), 'N/A') AS exp; // 10

答案 1 :(得分:0)

我怀疑它的功能顺序会给你带来麻烦。

尝试:

SELECT IF(billablerate IS NULL, 'N/A', CONVERT(billablerate, CHAR(5))) as billablerate FROM rates

答案 2 :(得分:0)

原来我的命令中的错误是语法错误,见下文:

我的原始代码:

SELECT CAST(IFNULL(billablerate, 'N/A'), CHAR(5)) AS billablerate FROM rates

修复:

SELECT CAST(IFNULL(billablerate, 'N/A') AS CHAR(5)) AS billablerate FROM rates