Friday 27 May 2011

Adding a Null value in SQL / Report Expression
When adding two fields together in a Microsoft Dynamics CRM 4.0 SSRS Report I was getting an issue where if one of the values being added was NULL, I would get the message #Error with the Matrix.
Below is an example of the Expression used within my Report.  In tests this worked fine when both fields had values, but if just one field was NULL the row would not calculate
=Fields!ap_taxtobeclaimed.Value + Fields!ap_taxclaimed.Value
To resolve this issue required me to use a SQL Function called COALESCE()
COALESCE() can be used to take an expression from SQL  and a value to use in the event that the values from the expression are NULL
COALESCE ( expression [ ,...n ] )
SELECT
(COALESCE(FilteredAp_transaction.ap_taxtobeclaimed,0) + COALESCE(FilteredAp_transaction.ap_taxclaimed,0)) AS TaxSum
,FilteredAp_transaction.ap_taxtobeclaimed
,FilteredAp_transaction.ap_taxclaimed
,year(FilteredAp_transaction.ap_date) as year
FROM
FilteredAp_transaction inner join
FilteredContact AS CRMAF_FilteredContact
on CRMAF_FilteredContact.contactid = FilteredAp_transaction.ap_contactnameid
Using the Coalesce expression I was able to display the correct Tax calculation per year



No comments:

Post a Comment