Handling of empty fields

Asked

Viewed 43 times

1

In this query, I have some stores that have not identified sale. Bringing the result as null. This impacts on the field of percentage and total. How could I make null fields to be interpreted as 0 and I can do my query calculations?

  select ti.Loja as Loja,ti.NomeLoja  as 'Nome Loja', ti.QtdTransacaoIdent as 
   'Identificado', 
    tni.QtdTransacaoNaoIdent as 'Não Identificado',
   SUM(QtdTransacaoIdent)+ SUM(QtdTransacaoNaoIdent)as Total,   
   CONVERT(VARCHAR(50),
   cast(QtdTransacaoIdent as money)/cast(QtdTransacaoIdent+ QtdTransacaoNaoIdent as money)*100)+' %' AS '% Engajamento'   
   from #temp_ident ti
   left join #temp_n_ident tni on tni.loja = ti.loja
   group by ti.Loja,ti.NomeLoja, ti.QtdTransacaoIdent,tni.QtdTransacaoNaoIdent
   order by Loja, 
    CONVERT(VARCHAR(50),
   cast(QtdTransacaoIdent as money)/cast(QtdTransacaoIdent+ QtdTransacaoNaoIdent as money)*100) 
   desc
  • Use the COALESCE function to consider zero where you have NULL.

1 answer

3


To replace the NULL field with a value, use ISNULL().

SELECT ISNULL(name, 'Não Encontrado') FROM bairros; 

Returns 'Not found' if name is NULL :)

More information about this function, access official documentation: Microsoft documentation

I hope I’ve helped..

  • 1

    Man, thank you so much, I ended up not thinking about this function, is null. It solved my problem as follows. If one of the stores has no identified or unidentified sale, the total sold on the day will be the value of the other store, for example: Extra Jundiai. 73 0 73 NULL Now I will adjust this for the percentage calculation.

  • 1

    I’m glad I could help.

Browser other questions tagged

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