Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 UDF help................ PLEASE!

Author  Topic 

chaz
Starting Member

4 Posts

Posted - 2005-09-13 : 18:35:37
I'm not very good at SQL UDFs.

I need one that tests for a NULL value and replaces that with a zero (0). If the field contain a value it stays.

The query I need it for would run something like this:

SELECT (ColumnA + ColumnB + ColumnC) AS GrandTotal FROM Table1

Column A,B & C (there are actually much more than three cloumns) are all money data types. Some sick, twisted B_______d, designed this table in such a way that A,B & C can and do contain NULLs rather than zero. I need to add them up.

Could some kind, generous soul, who knows the pain and suffering of having to work with ridiculously structured legacy databases from hell, PLEASE help me out on this? I know that this is probably painfully simple, but I've been beating myself up trying to get a UDF that will just pass a syntax check.

thanx

Chaz

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-09-13 : 18:38:59
You don't need a UDF for this, just use COALESCE.


SELECT COALESCE(ColumnA, 0) + COALESCE(ColumnB, 0) + COALESCE(ColumnC, 0) AS GrandTotal
FROM Table1

Tara
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-14 : 01:40:25
You can also use ISNULL as well

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -