从select语句返回值

时间:2013-10-25 05:38:13

标签: sql sql-server

我有这个查询字符串

  select a.name, a.address
  from table_main a
  where a.id=3345

对于此查询,我尝试添加a.amount如果为null则返回零,如果不返回某个值

 select a.name, a.addres, isnull(a.amount, 0) else 333
 from table_main a
 where a.id=3345

任何想法如何解决这个问题,以便如果a.amount为null,则返回零,如果不是返回值,即333 谢谢

5 个答案:

答案 0 :(得分:1)

您可以使用case声明

select a.name, 
       a.addres,
       case when a.amount is null 
            then 0 
            else 333
       end as amount_check
from table_main a
wher a.id = 3345

答案 1 :(得分:1)

select a.name, a.addres,
      case when a.amount is null then 0 else 333 end amount 
 from table_main a
 wher a.id=3345

答案 2 :(得分:1)

试试这个(Use Case-When语句)

select a.name, a.addres, case when a.amount is NULL then 0 else 333 end as Amount
from table_main a
wher a.id=3345

答案 3 :(得分:0)

您需要将其包围在CASE声明中

SELECT a.NAME,
   a.addres,
   CASE 
      WHEN a.amount IS NULL
         THEN 0
      ELSE 333
   END
FROM table_main a where a.id = 3345

答案 4 :(得分:0)

试试这个:

select a.name, a.addres,
case when a.amount is null then 0 else 333 end
from table_main a where a.id=3345