将 SQL 列的空值转换为 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13726276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:27:13  来源:igfitidea点击:

Convert SQL column null values to 0

sqlsql-serverdatabase

提问by user1820705

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.

我有这个观点,其中允许公式中的某些列为空。

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,因为如果它们为空,则公式的结果也将为空。

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

回答by Gordon Linoff

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

好吧,有人应该为 ANSI 标准代言:

coalesce(<column>, 0)

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

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

回答by huMpty duMpty

You can use ISNULL (Transact-SQL)

您可以使用ISNULL (Transact-SQL)

eg

例如

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

回答by Barry Kaye

Try this:

尝试这个:

ISNULL([nullable field], 0)