Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert SQL column null values to 0

I'm new to SQL Server and I have an issue.

I have this view, in which some of the columns from the formulas are allowed to be null.

How could I convert these null values to 0 because if they are null, the result of the formula it will be also null.

Thanks!

CREATE VIEW vwAchizitii
AS
    SELECT
            ac_id
           ,[Company]
           ,No
           ,[ContractID]
           ,[Seller]
           ,[AcquistionDate]
           ,[Village]
           ,[Commune]
           ,[Area]
           ,[PlotArea]
           ,[FieldNo]
           ,[Topo1]
           ,[Topo2]
           ,[Topo3]
           ,[Topo4]
           ,[Topo5]
           ,[TotalAreaSqm]
           ,[OwnershipTitle]
           ,[CadastralNO]
           ,[Type]
           ,[Price]
           ,[NotaryCosts]
           ,[LandTax]
           ,[OtherTaxes]
           ,[AgentFee]
           ,[CadastralFee]
           ,[TabulationFee]
           ,[CertSarcini]
           ,[ProcuraNO]
           ,(price+notarycosts+landtax+othertaxes+agentfee+cadastralfee+tabulationfee+certsarcini) as TotalCosts
           ,(price+notarycosts+landtax+othertaxes+agentfee+cadastralfee+tabulationfee+certsarcini)/(TotalAreaSqm/10000) as RonPerHa
           ,(price+notarycosts+landtax+othertaxes+agentfee+cadastralfee+tabulationfee+certsarcini)/(TotalAreaSqm/10000*FixHist) as EurPerHa
           ,[DeclImpunere]
           ,[FixHist]
           ,(price+notarycosts+landtax+othertaxes+agentfee+cadastralfee+tabulationfee+certsarcini)/FixHist as EurHist
           ,[LandStatus]
FROM      
   nbAchizitii
like image 221
user1820705 Avatar asked Nov 29 '22 02:11

user1820705


2 Answers

Well, someone should put in a word for ANSI standards:

coalesce(<column>, 0)

isNULL is specific to databases (and even does different things in some databases).

like image 137
Gordon Linoff Avatar answered Dec 10 '22 03:12

Gordon Linoff


You can use ISNULL (Transact-SQL)

eg

(isnull(price,0)+isnull(notarycosts,0)) as Total
like image 20
huMpty duMpty Avatar answered Dec 10 '22 03:12

huMpty duMpty