WhenI call this stored procedure:
ALTER PROCEDURE [dbo].[GetSorted]
(
@OrderByColumn nvarchar(256)
)
AS
SET NOCOUNT ON
SELECT itDocs.AddedDate, itDocs.AddedBy FROM itDocs
ORDER BY
CASE WHEN @OrderByColumn='AddedDate' THEN itDocs.AddedDate
WHEN @OrderByColumn='AddedBy' THEN itDocs.AddedBy
END ASC
I get error:
Conversion failed when converting date and/or time from character string
.
This is how I call SP:
DECLARE @return_value int
EXEC @return_value = [dbo].[GetSorted]
@OrderByColumn = 'AddedBy'
SELECT 'Return Value' = @return_value
GO
UPDATE: If I use multi-case approach as suggested in fist 2 answers, I get error when trying to added another case for orientation:
ORDER BY
CASE WHEN @OrderDirection=0 THEN
CASE WHEN @OrderByColumn='AddedDate' THEN itDocs.AddedDate END ASC,
CASE WHEN @OrderByColumn='AddedBy' THEN itDocs.AddedBy END ASC
end
CASE WHEN @OrderDirection=1 THEN