ThisIF @GroupLeader = '' THEN SELECT GRPLDR.GLPerson FROM dbo.Main LEFT JOIN ( SELECT Person AS GLPerson, WPId FROM SigAuth WHERE Title = 'GL' ) AS GRPLDR ON dbo.Main.WPID = GRPLDR.WPIDELSE SELECT GRPLDR.GLPerson FROM dbo.Main LEFT JOIN ( SELECT Person AS GLPerson, WPId FROM SigAuth WHERE Title = 'GL' AND Person LIKE @GroupLeader + '%' ) AS GRPLDR ON dbo.Main.WPID = GRPLDR.WPID
But all yuo really need is thisSELECT GRPLDR.GLPersonFROM dbo.MainLEFT JOIN ( SELECT Person AS GLPerson, WPId FROM SigAuth WHERE Title = 'GL' AND Person LIKE ISNULL(@GroupLeader, '') + '%' ) AS GRPLDR ON dbo.Main.WPID = GRPLDR.WPID
because when @GroupLeader is empty string (or null) it will get all records anyway.
E 12°55'05.25"N 56°04'39.16"