简体   繁体   English

在sql中联接两个表时将空值替换为0而不是null

[英]Replace empty values as 0 instead of null while joining two tables in sql

I have join two tables t1 and t2. 我已经加入了两个表t1和t2。 The output produces some null records since there is no data in the table t2. 由于表t2中没有数据,因此输出产生一些空记录。 Instead of showing null I want to show 0 since I have to perform some arithmetic operation in the crystal reports. 我不想显示null而是显示0,因为我必须在Crystal报表中执行一些算术运算。 please help me..... 请帮我.....

sample example 样例

declare @t table (ID int)
declare @t1 table (ID int)

insert into @t (id) values (1)

select t.ID,ISNULL(TT.ID,0)id  from @t t
LEFT JOIN @t1 tt
ON t.ID = tt.ID 

Use the COALESCE function which automatically replace null values as 0 . 使用COALESCE函数自动将null值替换为0 Sample 样品

SELECT COALESCE(total_amount, 0) from #Temp1

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

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