如果为null则返回零

时间:2013-09-10 17:47:19

标签: sql sql-server tsql

我有一个选择陈述

  select 
  name
  ,age
  from table_employees
  where id=@impId;

我想检查年龄是否为零而不是返回零。我试过跟随,但它不起作用

     select 
     name
     ,age isnull(age,0.00)
     from table_employees
     where id=@impId;

请让我知道如何解决这个问题。 谢谢

7 个答案:

答案 0 :(得分:2)

建议经常被忽视的COALESCE:

select 
   name, 
   coalesce(age, 0.00) as age_not_null 
from table_employees 
where id = @impId;

答案 1 :(得分:1)

在SQL Server 2005或更高版本中,您可以使用COALESCE函数:

SELECT
    name
,   COALESCE(age, 0) as age
FROM table_employees
WHERE id=@impId

该函数逐个计算其参数,并返回第一个非NULL值。

答案 2 :(得分:1)

试试这种方式

select 
name,age=isnull(age,0.00)
from table_employees
where id=@impId;

select 
name,
isnull(age,0.00) as age 
from table_employees
where id=@impId;

答案 3 :(得分:1)

你可以试试这个: -

select 
    name
,   COALESCE(age,0) as age
from table_employees
where id=@impId;

答案 4 :(得分:0)

请尝试以下:

 select 
 name
 ,isnull(age,0.00)
 from table_employees
 where id=@impId;

答案 5 :(得分:0)

尝试:

 SELECT name,isnull(age,0) 
 FROM table_employees
 WHERE id=@impId;

答案 6 :(得分:0)

ISNULL是SQL Server的语句。你不能把列名放在它之前。

所以,试试这个:

SELECT name, ISNULL(age,0.00) AS age
FROM table_employees
WHERE id=@impId;