0

I have to edit a stored procedure who has to return the sums of three columns having nullable values. If there is a null value, I need to cast it to 0

Here is a screenshot of data :

enter image description here

And here is the originial request using the first column only :

SELECT SUM(reglProj.Montant) /* SUM of 'Montant', 'FraisMagasing', 'FraisVendeur' instead */ AS SommeReglement
FROM Projet.LigneEcheancierProjet ligne
INNER JOIN Projet.ReglementProjetEcheance reglProj ON reglProj.LigneEcheancierProjetId = ligne.LigneEcheancierProjetId
....

Do you have some best practices using the sum and case conditions in T-SQL ?

4
  • Use isnull or coalesce. . . Commented Jul 21, 2015 at 13:44
  • where is your other 2 column in sql query , which you want to cast Commented Jul 21, 2015 at 13:45
  • 1
    Why would you want to do that? SUM ignores null values (basically treat them as 0) by default... Commented Jul 21, 2015 at 13:48
  • Sum ignores. That's true. But he wants to add values and when you add NULL then the result becomes NULL Commented Jul 21, 2015 at 13:57

3 Answers 3

3
--ANSI standard
    SELECT SUM(COALESCE(col1,0)) + SUM(COALESCE(col2,0)) + SUM(COALESCE(col3,0))

--SQL Server Style    
    SELECT SUM(ISNULL(col1,0)) + SUM(ISNULL(col2,0)) + SUM(ISNULL(col3,0))

--The one wthout functions. It will work the same as previous OR FASTER.    
    SELECT SUM(CASE WHEN col1 IS NULL THEN 0 ELSE col1 END) + SUM(CASE WHEN col2 IS NULL THEN 0 ELSE col2 END) + SUM(CASE WHEN col3 IS NULL THEN 0 ELSE col3 END)

Choose one for yourself.

OR you might need following (if you want to add sums by row):

--ANSI standard
    SELECT SUM(COALESCE(col1,0) +COALESCE(col2,0) + COALESCE(col3,0))

--SQL Server Style    
    SELECT SUM(ISNULL(col1,0)+ ISNULL(col2,0) + ISNULL(col3,0))

--The one wthout functions. It will work the same as previous OR FASTER.    
    SELECT SUM(CASE WHEN col1 IS NULL THEN 0 ELSE col1 END + CASE WHEN col2 IS NULL THEN 0 ELSE col2 END + CASE WHEN col3 IS NULL THEN 0 ELSE col3 END)
Sign up to request clarification or add additional context in comments.

1 Comment

Thx for the result. I edit my problem because I need to do the sum of my three columns (in the same table) : 'Montant', 'FraisMagasing', 'FraisVendeur'
1

In Sql Server, (and probably in most if not all relational databases) the SUM Aggregation function ignores null values by default, so there really is no need to use coalesce or isnull inside it.

If you want the sum of all 3 columns for every single row, then you need to use isnull:

SELECT ISNULL(reglProj.Montant,0) + 
       ISNULL(reglProj.FraisMagasing ,0) + 
       ISNULL(reglProj.FraisVendeur,0)
FROM Projet.LigneEcheancierProjet ligne
INNER JOIN Projet.ReglementProjetEcheance reglProj 
      ON reglProj.LigneEcheancierProjetId = ligne.LigneEcheancierProjetId

If you need the aggregated sum of all 3 columns you can simply do it like this:

SELECT ISNULL(SUM(reglProj.Montant), 0) + 
       ISNULL(SUM(reglProj.FraisMagasing), 0) + 
       ISNULL(SUM(reglProj.FraisVendeur), 0)
FROM Projet.LigneEcheancierProjet ligne
INNER JOIN Projet.ReglementProjetEcheance reglProj 
      ON reglProj.LigneEcheancierProjetId = ligne.LigneEcheancierProjetId

Comments

0

It seems you are looking for ISNULL actually

SELECT SUM( ISNULL(reglProj.Montant,0) + ISNULL(FraisMagasing,0)+ ISNULL(FraisVendeur,0))    AS SommeReglement
FROM Projet.LigneEcheancierProjet ligne
INNER JOIN Projet.ReglementProjetEcheance reglProj ON reglProj.LigneEcheancierProjetId = ligne.LigneEcheancierProjetId

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.