将SQL列空值转换为0

时间:2022-03-09 07:38:57

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

我是SQL Server的新手,我有一个问题。

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

我有这个观点,其中公式中的一些列被允许为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.

我怎么能将这些空值转换为0,因为如果它们为null,则公式的结果也将为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

3 个解决方案

#1


7  

You can use ISNULL (Transact-SQL)

您可以使用ISNULL(Transact-SQL)

eg

例如

(isnull(price,0)+isnull(notarycosts,0)) as Total

#2


10  

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

好吧,有人应该为ANSI标准说一句话:

coalesce(<column>, 0)

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

isNULL特定于数据库(甚至在某些数据库中做不同的事情)。

#3


5  

Try this:

尝试这个:

ISNULL([nullable field], 0)

#1


7  

You can use ISNULL (Transact-SQL)

您可以使用ISNULL(Transact-SQL)

eg

例如

(isnull(price,0)+isnull(notarycosts,0)) as Total

#2


10  

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

好吧,有人应该为ANSI标准说一句话:

coalesce(<column>, 0)

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

isNULL特定于数据库(甚至在某些数据库中做不同的事情)。

#3


5  

Try this:

尝试这个:

ISNULL([nullable field], 0)