I need help with this query. I need to sort this query by the CallDate column which is of Varchar datatype, not Datetime. When I try to sort by CallDate descending, it's not sorting properly. I believe the issue is that the column is of data type varchar. How can I convert the column to date so that it sorts properly? I have included my T-SQL code, and also what my query is currently returning (which is wrong).
Thanks in advance for your help.
Here's my code sample:
DECLARE @AgentID varchar(50), @StartDate DateTime, @EndDate DateTime
SET @AgentID = '456'
SET @StartDate = '9/1/2013'
SET @EndDate = '9/16/2013'
WHERE CallDate >= @StartDate AND CallDate <= @EndDate
AND AgentID = @AgentID
ORDER BY CallDate DESC
I GET THE FOLLOWING RESULTS (which is not sorting properly by CallDate descending)