简体   繁体   English

将SQL列空值转换为0

[英]Convert SQL column null values to 0

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

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特定于数据库(甚至在某些数据库中做不同的事情)。

You can use ISNULL (Transact-SQL) 您可以使用ISNULL(Transact-SQL)

eg 例如

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

尝试这个:

ISNULL([nullable field], 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM