Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

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

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 NULL how can I show price as 0 ?

What should be mention in SELECT statement to make NULL as 0 ?

like image 390
Neo Avatar asked May 21 '13 09:05

Neo


People also ask

How do I return 0 instead of NULL in SQL?

We can return 0 for NULL in MySQL with the help of IFNULL() method. The syntax of IFNULL() is as follows. IFNULL(YOUREXPRESSION,0);

How do I avoid NULL values in select query?

SELECT column_names FROM table_name WHERE column_name IS NOT NULL; Query: SELECT * FROM Student WHERE Name IS NOT NULL AND Department IS NOT NULL AND Roll_No IS NOT NULL; To exclude the null values from all the columns we used AND operator.

Is NULL equal to 0 in SQL?

In SQL Server, NULL value indicates an unavailable or unassigned value. The value NULL does not equal zero (0), nor does it equal a space (' '). Because the NULL value cannot be equal or unequal to any value, you cannot perform any comparison on this value by using operators such as '=' or '<>'.

How do you replace NULL values in SQL?

The ISNULL Function is a built-in function to replace nulls with specified replacement values. To use this function, all you need to do is pass the column name in the first parameter and in the second parameter pass the value with which you want to replace the null value.


1 Answers

Use coalesce():

select  coalesce(Eprice, 0) as Eprice 

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

select  isnull(Eprice, 0) as Eprice 
like image 192
Andomar Avatar answered Sep 21 '22 15:09

Andomar