I have an stored procedure and I am using this variable i.e. @orderBy.
Here is my code below:
ORDER BY
CASE
WHEN @orderBy = 'ClientName' THEN c.LastName
WHEN @orderBy = 'EmployerName' THEN cda.Name
WHEN @orderBy = 'EmploymentDate' THEN cda.StartDate
END
The problem is when I’m using the @orderBy variable, it is a varchar and it is giving error on the third line:
Conversion failed when converting character string to smalldatetime data type.
Working fine if I comment third line i.e. WHEN @orderBy = 'EmploymentDate' THEN cda.StartDate
How do I get past this datatype conversion error?
Thanks in advance.
You can use a case-per-column to remove the mixed types;