sqlsql-server-2008sql-null

replace NULL with Blank value or Zero in sql server


I have a temp table and in that one of my column total_amount is of integer type and NOT NULL. While querying data, I received NULL values for total_Amount column.

How ever I used following syntax to remove nulls but some how still NULL values appearing, correct me if I am wrong.

Create table #Temp1
(
    issue varchar(100) NOT NULL,
    total_amount int NOT NULL
) 

This is my query

Case when total_amount = 0 then 0  
else isnull(total_amount, 0)  
end as total_amount  

I am facing issue at my else part.


Solution

  • You can use the COALESCE function to automatically return null values as 0. Syntax is as shown below:

    SELECT COALESCE(total_amount, 0) from #Temp1