T SQL Coalesece instead of Case

This query calculates the AdjValue by subtracting TaxDepr from DpreciableBasis. To get the TaxDepr I call a function passing an EquipOid and a date. The date is converted to the 1st day of the next month. If the result of the function is 0.00 then I grab the value from another table. Can I use the Coalesce expression instead of a Case statement? How would this be done?    Thanks.



                      ,AdjValue = isnull([ED1].TaxDepreciationDepreciableBasis,0)-
                        
                              (
                              select      
                                    
                              case
                              when      isnull(dbo.GetEquipmentTotalTaxDeprAmtFN
                                    (E1.EquipmentOid,CONVERT(varchar(7), DATEADD(m, 1, ff1.DateFunded), 23) + '-01'),0.00) = 0.00
            
                              then isnull([ED1].TaxDepreciationLifetime,0.00)      
                              
                              else
                                    (dbo.GetEquipmentTotalTaxDeprAmtFN
                                    (E1.EquipmentOid,CONVERT(varchar(7), DATEADD(m, 1, ff1.DateFunded), 23) + '-01'))
            
                              
                              end
                              )
ryannAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
You shouldn't need the Coalesce function. The IsNull function should be sufficient:
isnull(dbo.GetEquipmentTotalTaxDeprAmtFN(E1.EquipmentOid,CONVERT(varchar(7), DATEADD(m, 1, ff1.DateFunded), 23) + '-01'), dbo.GetEquipmentTotalTaxDeprAmtFN(E1.EquipmentOid,CONVERT(varchar(7), DATEADD(m, 1, ff1.DateFunded), 23) + '-01'))

If you have a concern that the first part of the ISNULL function will return 0.00, you can wrap that part up in the NullIF function.
Kyle AbrahamsSenior .Net DeveloperCommented:
can GetEquipmentTotalTaxDeprAmtFN actually return 0?  (not null)

Coalesce will only help you if you're returning null values.
ryannAuthor Commented:
My Case statement works fine. A person told me that Coalesce would be another option I could use and would make the code easier to read. I am just curious how I would code this query using coalesce instead of a Case statement.
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Shaun KlineLead Software EngineerCommented:
Missed the second IsNULL.

You can do this:
COALESCE(dbo.GetEquipmentTotalTaxDeprAmtFN
                                     (E1.EquipmentOid,CONVERT(varchar(7), DATEADD(m, 1, ff1.DateFunded), 23) + '-01'), [ED1].TaxDepreciationLifetime,0.00)
Kyle AbrahamsSenior .Net DeveloperCommented:
The above assumes GetEquipmentTotalTaxDeprAmtFN will only return nulls and not 0.  If GetEquipmentTotalTaxDeprAmtFN can return a [ED1].TaxDepreciationLifetime will not be used as in your case.

I guess you could combine with a nullif:

COALESCE(nullif(dbo.GetEquipmentTotalTaxDeprAmtFN
                                     (E1.EquipmentOid,CONVERT(varchar(7), DATEADD(m, 1, ff1.DateFunded), 23) + '-01'),0), [ED1].TaxDepreciationLifetime,0.00)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ryannAuthor Commented:
Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.