DECLARE @SenderUserName NVARCHAR(50)
DECLARE @Yesterday DATE
SET @SenderUserName = N'sonam'
SET @Yesterday = DATEADD(DAY , -1 , GETDATE())

SELECT     MessageID, SenderUserName, ReplyTo, GroupName, MessageDate, MessageBudy
FROM   (SELECT     dbo.tblMessageToGroups.MessageID, 
dbo.tblUsers.IUserName AS SenderUserName, 
tblUsers_1.IUserName AS ReplyTo, dbo.tblGroups.GroupName, 
   CONVERT(date,dbo.tblMessageToGroups.MessageDateTime) AS MessageDate, 
   dbo.tblMessageToGroups.MessageBudy
  FROM         dbo.tblMessageToGroups INNER JOIN
  dbo.tblGroups ON dbo.tblMessageToGroups.GroupID = dbo.tblGroups.GroupID INNER JOIN
  dbo.tblUsers ON dbo.tblMessageToGroups.SenderID = dbo.tblUsers.IUserID LEFT OUTER JOIN
 dbo.tblUsers AS tblUsers_1 ON dbo.tblMessageToGroups.ReplyToUserID = 
 tblUsers_1.IUserID) AS D
WHERE     ((D.SenderUserName = @SenderUserName) OR (D.ReplyTo = @SenderUserName)) 
AND (D.MessageDate = @Yesterday)


