简体   繁体   中英

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

I have join two tables t1 and t2. The output produces some null records since there is no data in the table t2. Instead of showing null I want to show 0 since I have to perform some arithmetic operation in the crystal reports. 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 . Sample

SELECT COALESCE(total_amount, 0) from #Temp1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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