我如何使用 SELECT 语句 sql 在结果中显示“0”而不是 NULL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16667148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:20:10  来源:igfitidea点击:

Instead of NULL how do I show `0` in result with SELECT statement sql?

sqlsql-servertsql

提问by Neo

I have one stored procedurewhich is giving me an output (I stored it in a #temp table) and that output I'm passing to another scalar function.

我有一个stored procedure给我一个输出(我将它存储在一个 #temp 表中)并且我将这个输出传递给另一个scalar function.

Instead of NULL how do I show 0in result with SELECT statement sql?

我如何0使用 SELECT 语句 sql 在结果中显示而不是 NULL ?

For example stored proc is having select statement like follwing :

例如,存储过程有如下选择语句:

SELECT Ename , Eid , Eprice , Ecountry from Etable
Where Ecountry = 'India'

Which is giving me output like

这给了我这样的输出

Ename    Eid    Eprice    Ecountry
Ana      12     452       India
Bin      33     NULL      India
Cas      11     NULL      India

Now instead of showing NULLhow can I show price as 0?

现在而不是显示NULL我如何将价格显示为0

What should be mention in SELECTstatement to make NULLas 0?

SELECT声明中应该提到什么NULL作为0

回答by Andomar

Use coalesce():

使用coalesce()

select  coalesce(Eprice, 0) as Eprice

In SQL Server only, you can save two characters with isnull():

仅在 SQL Server 中,您可以使用以下命令保存两个字符isnull()

select  isnull(Eprice, 0) as Eprice

回答by Ankur Sharma

Try these three alternatives:

试试这三个替代方案:

1. ISNULL(MyColumn, 0)

2. SELECT CASE WHEN MyColumn IS NULL THEN 0 ELSE MyColumn END FROM MyTable

3. SELECT COALESCE(MyCoumn, 0) FROM MyTable

There is another way but it is not supported by most of the databases SELECT MyColumn + 0 This might work, but NULL + anything is still NULL in T-SQL.

还有另一种方法,但大多数数据库不支持它 SELECT MyColumn + 0 这可能有效,但 NULL + 任何东西在 T-SQL 中仍然是 NULL。

回答by TechDo

Try ISNULL(Eprice, 0)instead of Eprice

尝试ISNULL(Eprice, 0)代替Eprice

回答by user707727

You could use this:

你可以用这个:

SELECT Ename , Eid , ISNULL(Eprice, 0) as Eprice, Ecountry from Etable
Where Ecountry = 'India'