|
-
Aug 11th, 2004, 12:25 PM
#1
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?
-
Aug 11th, 2004, 12:28 PM
#2
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"
-
Aug 11th, 2004, 12:30 PM
#3
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().
-
Aug 11th, 2004, 01:03 PM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 11th, 2004, 01:08 PM
#5
That would be cleaner. I'll try it out later. Tnx
-
Aug 11th, 2004, 01:11 PM
#6
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"
-
Aug 11th, 2004, 02:33 PM
#7
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 11th, 2004, 02:44 PM
#8
Hyperactive Member
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
-
Aug 11th, 2004, 03:17 PM
#9
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 11th, 2004, 03:33 PM
#10
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"
-
Aug 11th, 2004, 03:41 PM
#11
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 11th, 2004, 04:29 PM
#12
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
-
Aug 12th, 2004, 12:20 AM
#13
Hyperactive Member
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
-
Aug 12th, 2004, 05:14 AM
#14
Addicted Member
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
-
Aug 12th, 2004, 08:35 AM
#15
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
-
Aug 16th, 2004, 03:26 AM
#16
Hyperactive Member
I stand corrected, I recently had this code and I was doing a different thing...
again...
my apologies...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|