The below stored proc works fine except for the fact that when I uncomment the second part of the date check in the 'where' clause it blows up on a date conversion even if the passed in keyword is null or '111'.
I'm open to any suggestions on how to do this dynamic where clause differently.
I appreciate any help.
ALTER PROCEDURE [SurveyEngine].[GetPageOf_CommentsOverviewRowModel]
@sortColumn varchar(50),
@isASC bit,
@keyword varchar(50)
AS
BEGIN
declare @keywordType varchar(4)
set @keywordType = null
if ISDATE(@keyword) = 1
set @keywordType = 'date'
else if ISNUMERIC(@keyword) = 1
set @keywordType = 'int'
select c.CommentBatch BatchID, c.CreatedDate DateReturned, COUNT(c.CommentID) TotalComments
from SurveyEngine.Comment c
where (@keywordType is null)
or (@keywordType = 'date') --and c.CreatedDate = @keyword)
or (@keywordType = 'int' and (CONVERT(varchar(10), c.CommentBatch) like @keyword+'%'))
group by c.CommentBatch, c.CreatedDate
order by case when @sortColumn = 'BatchID' and @isASC = 0 then c.CommentBatch end desc,
case when @sortColumn = 'BatchID' and @isASC = 1 then c.CommentBatch end,
case when @sortColumn = 'DateReturned' and @isASC = 0 then c.CreatedDate end desc,
case when @sortColumn = 'DateReturned' and @isASC = 1 then c.CreatedDate end,
case when @sortColumn = 'TotalComments' and @isASC = 0 then COUNT(c.CommentID) end desc,
case when @sortColumn = 'TotalComments' and @isASC = 1 then COUNT(c.CommentID) end
END