SELECT LicenceIssueDetailID,ReferenceNumber FROM App.LicenceIssueDetails LEFT JOIN Wf.WorkflowInstances ON Wf.WorkflowInstances.WorkflowInstanceID=App.LicenceIssueDetails.WorkflowInstanceID  WHERE (CASE WHEN isnull(@SearchText,'')<>'' THEN CASE WHEN App.LicenceIssueDetails.LicenceIssueDetailID LIKE '%' + @SearchText + '%' THEN 1  WHEN Wf.WorkflowInstances.ReferenceNumber LIKE '%' + @SearchText + '%' THEN 1  ELSE 0 END ELSE 0 END)=1