Sum null value Laravel

Asked

Viewed 31 times

1

I am trying to sum the following with Query Builder, but if one of the values is null and does not sum

DB::raw("estudantes_carga_horaria.dias_letivos_oferecidos + estudantes_carga_horaria.dias_letivos_oferecidos_outra_escola AS total_dias_letivos_oferecidos")

1 answer

1


You can use the Coalesce, which will 0 when the value is null.

DB::raw("
COALESCE(estudantes_carga_horaria.dias_letivos_oferecidos, 0) + 
COALESCE(estudantes_carga_horaria.dias_letivos_oferecidos_outra_escola, 0)
AS total_dias_letivos_oferecidos")

I’m answering what you asked, but possibly there are other ways to do it depending on what you want with this value.

  • It worked perfectly! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.