The following is not quite working the way it needs.If the importalertID is not found in the table invoicenotadvanced,it should return -1.The way it is now, it returns -1 if it's not there OR it's 0 and is there.It must return -1 ONLY if the importalertID does NOT exist in that table.SELECT a.ImportAlertID,ISNULL( (SELECT COUNT(*) FROM invoicenotadvanced WHERE((creditlimitapproved IS NULL OR creditlimitapproved = 0) OR (CBDateApproved IS NULL OR CBDateApproved = 0))AND a.ImportAlertID = ImportAlertID), -1)AS fundingRuleFail FROM ImportAlert a INNER JOIN ImportType b ON a.ImportTypeID = b.ImportTypeID INNER JOIN Client c ON a.ClientID = c.pk_id WHERE ( (@clientID IS NULL OR @clientID = '' OR a.ClientID = @clientID) AND (@type IS NULL OR @type = '' OR a.ImportTypeID = @type) AND (@days IS NULL OR @days = '' OR DATEDIFF(day, a.CompletedDate, GETDATE()) <= @days) ) AND a.CompleteStatus = 1
Any suggestions?Thanks,Zath