barnesco
asked on
Dynamic Order By and ASC DESC
Referencing: stackoverflow.com/question s/1147763/ dynamic-or der-direct ion
I attempted to set mine up the same way, but I'm receiving the error message: Conversion failed when converting the varchar value 'StartDate' to data type int.
The below is what I have:
@OrderBy varchar(10) = 'StartDate',
@Direction int = 1
...
CASE WHEN @Direction = 0 THEN 1
ELSE
CASE WHEN CAST(@OrderBy AS varchar(10)) = 'StartDate' THEN 'StartDate'
WHEN CAST(@OrderBy AS varchar(10)) = 'EndDate' THEN 'EndDate'
END
END ASC,
CASE WHEN @Direction = 1THEN 1
ELSE
CASE WHEN CAST(@OrderBy AS varchar(10)) = 'StartDate' THEN 'StartDate'
WHEN CAST(@OrderBy AS varchar(10)) = 'EndDate' THEN 'EndDate'
END
END DESC
I attempted to set mine up the same way, but I'm receiving the error message: Conversion failed when converting the varchar value 'StartDate' to data type int.
The below is what I have:
@OrderBy varchar(10) = 'StartDate',
@Direction int = 1
...
CASE WHEN @Direction = 0 THEN 1
ELSE
CASE WHEN CAST(@OrderBy AS varchar(10)) = 'StartDate' THEN 'StartDate'
WHEN CAST(@OrderBy AS varchar(10)) = 'EndDate' THEN 'EndDate'
END
END ASC,
CASE WHEN @Direction = 1THEN 1
ELSE
CASE WHEN CAST(@OrderBy AS varchar(10)) = 'StartDate' THEN 'StartDate'
WHEN CAST(@OrderBy AS varchar(10)) = 'EndDate' THEN 'EndDate'
END
END DESC
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER