```dart create or replace function calculate_curre...
# sql
m
Copy code
dart
create or replace function calculate_current_balance(acc_name text)

returns numeric as $$

declare
  debit_total numeric;
  credit_total numeric;

begin
  select SUM(amount)
  into debit_total
  from transactions
  where transactions."debitAccount" = acc_name;


  select SUM(amount)
  into credit_total
  from transactions
  where transactions."creditAccount" = acc_name;



  if debit_total=null
  then
    debit_total=0;
  end if;
  if credit_total=null
  then
    credit_total=0;
  end if;

  return credit_total-debit_total;

end;

$$ language plpgsql
When there is no row containing the
acc_name
in any one of the two queries, it returns
null
. To get rid of that, I added these if statements but I am still getting
null
. Any idea how I can fix this?
g
You must test for null with value IS NULL. = does not work as you can't compare to nothing.... https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-is-null/
m
I did not know that was a thing.
I just replaced
=
with
is
and that gives the correct output.
Thanks