1

我有一个选择语句

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

我想检查年龄是否为空而不是返回零。我尝试了以下但它不起作用

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

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

4

7 回答 7

1

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

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

该函数一个一个地计算它的参数,并返回第一个非NULL值。

于 2013-09-10T17:49:52.090 回答
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;
于 2013-09-10T17:49:56.623 回答
1

你可以试试这个: -

select 
    name
,   COALESCE(age,0) as age
from table_employees
where id=@impId;
于 2013-09-10T17:51:11.363 回答
1

建议经常被忽视的 COALESCE:

select 
   name, 
   coalesce(age, 0.00) as age_not_null 
from table_employees 
where id = @impId;
于 2013-09-10T17:51:32.823 回答
0

请尝试以下操作:

 select 
 name
 ,isnull(age,0.00)
 from table_employees
 where id=@impId;
于 2013-09-10T17:48:50.003 回答
0

尝试:

 SELECT name,isnull(age,0) 
 FROM table_employees
 WHERE id=@impId;
于 2013-09-10T17:49:04.957 回答
0

ISNULL是 SQL Server 的语句。您不能将列名放在它前面。

所以,试试这个:

SELECT name, ISNULL(age,0.00) AS age
FROM table_employees
WHERE id=@impId;
于 2013-09-10T18:03:23.703 回答