当我选择的值等于0时,我希望得到'-'
而不是0。
Qa01
字段是一个字符串。 (我必须将其转换为整数以用于SUM函数)
我试过这个:
SELECT [Code article],
Division,
count(LigneFab),
IIF(sum(cast(Qa01 as int))='0','-',sum(cast(Qa01 as int))) as qa01
from dbo.PDP
where [Code article] = '10000100510'
and Division = '1000'
group by [Code article],
Division
如果Qa01值为'10',则显示'10',但如果值为'0',则显示'0'而不是' - ',因此我的查询无效...
有没有办法显示' - '而不是'0'?
感谢您的帮助
答案 0 :(得分:2)
您要与字符串'0'
进行比较而不是数字0
将其更改为
IIF(sum(cast(Qa01 as int)) = 0,'-',sum(cast(Qa01 as int))) as qa01
答案 1 :(得分:1)
你需要小心你的类型。例如,您使用单引号0
,这是不必要的。
(CASE WHEN sum(cast(Qa01 as int)) = 0 THEN
ELSE CAST(sum(cast(Qa01 as int)) as VARCHAR(32))
END) as qa01
注意:
CASE
是条件表达式的ANSI标准。 IIF()
用于向后兼容MS Access。答案 2 :(得分:0)
使用case -
(case(Convert(Varchar,sum(Qa01)) when '0' then '-' else Convert(Varchar,sum(Qa01)) end) as qa01
答案 3 :(得分:0)
感谢您的帮助,
这是适用于我的解决方案:
SELECT [Code article],Division,count(LigneFab),
(CASE (CASE cast(sum(cast(Qa01 as int)) as nvarchar(50))
WHEN '0'
THEN '-'
ELSE (CASE cast(sum(cast(Qa01 as int)) as nvarchar(50)) END) as Qa01
from dbo.PDP where [Code article] = '10000100510' and Division = '1000'
group by [Code article],Division
我必须在nvarchar中将Qa01的总和转换回来,所以当我想要Qa01 ='0'时它会显示' - '。