92
SELECT 
    sum(TotalHoursM)
          + (TotalHoursT)
          + (TotalHoursW)
          + (TotalHoursTH)
          + (TotalHoursF) 
          AS TOTAL
FROM LeaveRequest
1
  • 2
    SQL Server not tagged ? Commented Mar 22, 2018 at 15:39

9 Answers 9

125

If the column has a 0 value, you are fine, my guess is that you have a problem with a Null value, in that case you would need to use IsNull(Column, 0) to ensure it is always 0 at minimum.

Sign up to request clarification or add additional context in comments.

6 Comments

Yonita is Michael's answer answered your question mark it as answered by using the check mark.
"Yonita is Michael's answer answered your question mark it as answered by using the check mark" -- my brain took an age to parse this sentence.
In MySQL, ISNULL seems to be just a 1-parameter boolean function, but as in omkarv's answer, IFNULL does the job.
IsNull is not portable, and the question doesn't specify a database. See below for COALESCE, which is portable (if not always optimal in performance).
The question is on SQL, but the IsNull function indicated is not an SQL primitive.
|
101

The previous answers using the ISNULL function are correct only for MS Sql Server. The COALESCE function will also work in SQL Server. But will also work in standard SQL database systems. In the given example:

SELECT sum(COALESCE(TotalHoursM,0))
          + COALESCE(TotalHoursT,0)
          + COALESCE(TotalHoursW,0)
          + COALESCE(TotalHoursTH,0)
          + COALESCE(TotalHoursF,0) AS TOTAL FROM LeaveRequest

This is identical to the ISNULL solution with the only difference being the name of the function. Both work in SQL Server but, COALESCE is ANSI standard and ISNULL is not. Also, COALESCE is more flexible. ISNULL will only work with two parameters. If the first parameter is NULL then the value of the second parameter is returned, else the value of the first is returned. COALESCE will take 2 to 'n' (I don't know the limit of 'n') parameters and return the value of the first parameter that is not NULL. When there are only two parameters the effect is the same as ISNULL.

5 Comments

Additionally, Oracle SQL does not have ISNULL. Though, in my experience, you may get better results with NVL(): docs.oracle.com/cd/B19306_01/server.102/b14200/functions105.htm
Just the one i was looking for - postgres doesn't have isnull ether.
Isnull didn't work as expected in MySQL. Coalesce did the job. Thanks.
IsNull is not a SQL primitive. Therefore the other answers are not correct, only this one.
ISNULL vs COALESCE ?
17
SELECT sum(isnull(TotalHoursM,0)) 
         + isnull(TotalHoursT,0) 
         + isnull(TotalHoursW,0) 
         + isnull(TotalHoursTH,0) 
         + isnull(TotalHoursF,0))
AS TOTAL FROM LeaveRequest

Comments

7

Just for reference, the equivalent statement for MySQL is: IFNull(Column,0).

This statement evaluates as the column value if not null, otherwise it is evaluated as 0.

Comments

5

You can use ISNULL:

ISNULL(field, VALUEINCASEOFNULL)

Comments

2

looks like you want to SUM all the columns (I'm not sure where "sum 3 columns" comes from), not just TotalHoursM, so try this:

SELECT 
    SUM(    ISNULL(TotalHoursM  ,0)
          + ISNULL(TotalHoursT  ,0)
          + ISNULL(TotalHoursW  ,0)
          + ISNULL(TotalHoursTH ,0)
          + ISNULL(TotalHoursF  ,0) 
       ) AS TOTAL
    FROM LeaveRequest

1 Comment

thanks for the down vote almost 8 years after answering this! ha ha, this is the exact same answer given by others on this very question. I answered before them, yet they have lots of up votes.
1

You can also use nvl(Column,0)

1 Comment

nvl is specific to oracle pl/sql.
0

I would try this:

select sum (case when TotalHousM is null then 0 else TotalHousM end)
       + (case when TotalHousT is null then 0 else TotalHousT end)
       + (case when TotalHousW is null then 0 else TotalHousW end)
       + (case when TotalHousTH is null then 0 else TotalHousTH end)
       + (case when TotalHousF is null then 0 else TotalHousF end)
       as Total
From LeaveRequest

2 Comments

However I like this solution because it would also be able to take care of those cases where the value is blank and not NULL. You would have to add the LEN() > 0.
What is the difference between "being blank" and "NULL"? Thank you.
-3

If you want to avoid the null value use IsNull(Column, 1)

1 Comment

IsNull(Column, 1) will give wrong results. It will increase the sum / total by 1 for every null value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.