Results 1 to 16 of 16

Thread: SQL Server: Sum() Null -> 0 for calculation

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    SQL Server: Sum() Null -> 0 for calculation

    I had an issue awhile ago with SQL Server... I had wanted to do SUM() - SUM() but the second sum returned null (0 records). In access I could have done IIF(SUM() Is Null, 0, Sum()), how would I implement the same thing with SQL Server?

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    CASE WHEN SUM(.....) IS NULL THEN 0 ELSE SUM(.....)



    btw: in Access Nz(Sum(....),0)

    Nz is like an IIF for nulls
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Is Nz available in all versions? I can't recall correctly if it was Nz exactly but I saw a similarly spelled function in the help but it couldnt be recorgnized in the access 9? query hence I used IIF().

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    In SQL you could also use the IsNull function.
    Code:
    IsNull(Sum(Field1),0)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    That would be cleaner. I'll try it out later. Tnx

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    RobDog888 - Good to know!
    didnt realize Isnull could funtion like that in SQL.

    Thanks!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    No prob. guys.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Coalesce ?

    Isn't Coalesce the SQL equivalent of NZ?
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    From SQL BOL:

    COALESCE
    Returns the first nonnull expression among its arguments.

    Syntax
    COALESCE ( expression [ ,...n ] )

    Arguments
    expression

    Is an expression of any type.

    n

    Is a placeholder indicating that multiple expressions can be
    specified. All expressions must be of the same type or must be
    implicitly convertible to the same type.

    Return Types
    Returns the same value as expression.

    Remarks
    If all arguments are NULL, COALESCE returns NULL.

    COALESCE(expression1,...n) is equivalent to this CASE function:

    CASE
    WHEN (expression1 IS NOT NULL) THEN expression1
    ...
    WHEN (expressionN IS NOT NULL) THEN expressionN
    ELSE NULL

    Same or not same as nz? I'm not familiar with nz.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    Nz(Value, Val If Null, Vall If not Null)

    Nz(x,0,x)


    if x is null then 0 is returned, otherwize x
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    So then its similar, but not the same?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    IsNull(field,0) and Coalesce(field,0) will do the same thing - if field is null, the second item - 0 - is returned.

    IsNull is frowned upon in purist SQL, since it's a MS function, not standard on other SQL's.

    I use ISNULL anyway - I can't type Coalesce without thinking - everytime

  13. #13
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    ISNULL and COALESCE are not always the same

    SELECT ISNULL(SUM(Field1), 0) FROM Table1
    SELECT COALESCE(SUM(Field1), 0) FROM Table1

    are the same but not this:

    SELECT ISNULL(Field1, 0) FROM Table1
    SELECT COALESCE(Field1, 0) FROM Table1

    the first select statement will return ZERO if table1 is empty (or if you have WHERE clause and it returns empty for that matter)
    the second will give you NULL if table 1 is empty
    live, code and die...

  14. #14
    Addicted Member Bregalad's Avatar
    Join Date
    Jul 2000
    Location
    Oslo,Norway
    Posts
    183
    Also COALESCE can be used for multiple values:

    SELECT COALESCE(Field1,Field2,Field3,0) AS Result FROM Table1

    This will return the first of the fields that isn't NULL,
    or the number 0 if they all are NULL

  15. #15
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Originally posted by dRAMmer
    ISNULL and COALESCE are not always the same

    SELECT ISNULL(SUM(Field1), 0) FROM Table1
    SELECT COALESCE(SUM(Field1), 0) FROM Table1

    are the same but not this:

    SELECT ISNULL(Field1, 0) FROM Table1
    SELECT COALESCE(Field1, 0) FROM Table1

    the first select statement will return ZERO if table1 is empty (or if you have WHERE clause and it returns empty for that matter)
    the second will give you NULL if table 1 is empty
    I tested what you said here - and it sure didn't behave the way you said it would!

    Try this in QUERY ANALYZER:

    Code:
    GO
    DROP TABLE NULLTEST_T
    Go
    
    CREATE TABLE NULLTEST_T
    (RptSP		varchar(50)	NULL
    ,RptSeq		int		NULL)
    GO
    SELECT ISNULL(RPTSP,'IT IS NULL') FROM NULLTEST_T       -- These both return NO RECORDSET AT ALL
    SELECT COALESCE(RPTSP,'IT IS NULL') FROM NULLTEST_T
    GO
    SELECT ISNULL(RPTSEQ,0) FROM NULLTEST_T                    -- These both return NO RECORDSET AT ALL
    SELECT COALESCE(RPTSEQ,0) FROM NULLTEST_T
    GO
    INSERT INTO NULLTEST_T VALUES (NULL,NULL)
    GO
    SELECT ISNULL(RPTSP,'IT IS NULL') FROM NULLTEST_T       -- These both return "IT IS NULL"
    SELECT COALESCE(RPTSP,'IT IS NULL') FROM NULLTEST_T
    GO
    SELECT ISNULL(RPTSEQ,0) FROM NULLTEST_T                    -- These both return "0"
    SELECT COALESCE(RPTSEQ,0) FROM NULLTEST_T
    GO

  16. #16
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    I stand corrected, I recently had this code and I was doing a different thing...

    again...

    my apologies...
    live, code and die...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width