I want to to make to make the ordering in my query conditional so if it satisfiess the condition it should be ordered by descending
For instance:
SELECT * FROM Data ORDER BY SortOrder CASE WHEN @Direction = 1 THEN DESC END
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Don’t change the
ASCorDESC, change the sign of the thing being sorted-by:The OP asks:
id is just whatever column you’re sorting by; -id is just the negation of that, id * -1. If you’re sorting by more than one column, you’ll need to negate each column:
If you’re ordering by a non numeric column, you’ll need to find an expression that makes that column ‘negative’; writing a function to do that may help.