0

How can I make this correct? I am getting an error saying: Incorrect syntax near the keyword 'DESC'.

SELECT *
FROM Companies
Order By 
    CASE @OrderByField
        WHEN 'CompanyName' THEN CompanyName
        WHEN 'CreatedDate' THEN CreatedDate
    END, 
    CASE @Direction
        WHEN 'DESC' THEN DESC
        WHEN 'ASC' THEN ASC
    END 

Can I not have two case statements? If not, how can i pass in the name of the order by field and direction as parameters?

Thanks!

Another problem surface after the first one is solved...

If I include a field that doesn't have the datatype of string, if throws an error. For example:

SELECT *
FROM Companies
Order By
  CASE @Direction WHEN 'DESC' THEN
    CASE @OrderByField
        WHEN 'CompanyName' THEN CompanyName
        WHEN 'CreatedDate' THEN CreatedDate
        WHEN 'Score' THEN Score

    END
  END DESC,
  CASE @Direction WHEN 'ASC' THEN
    CASE @OrderByField
        WHEN 'CompanyName' THEN CompanyName
        WHEN 'CreatedDate' THEN CreatedDate
        WHEN 'Score' THEN Score
    END
  END ASC

@OrderByField is type of nvarchar(50) assume Score has a datatype of float.

Above throws an error like the one below even if i am not trying to order by the score field. Error converting data type nvarchar to float.

Similarly, including a createddate throws an error: Conversion failed when converting date and/or time from character string.

Will be very appreciated if anyone can help out.

4
  • stackoverflow.com/questions/758655/… Commented Jun 22, 2014 at 23:27
  • stackoverflow.com/questions/3884884/… Commented Jun 22, 2014 at 23:28
  • i will be appreciated if you can find a post on this as well! Commented Jun 23, 2014 at 0:09
  • See answer by LarsW on the first link, or case part of answer by gbn on second link; those seem to be exactly what you're after? Commented Jun 23, 2014 at 0:30

2 Answers 2

2

You can't return a keyword from a case statement.

But you can achieve what you want by ordering in two ways, but return a constant expression for the order you don't want:

SELECT *
FROM Companies
Order By
  CASE @Direction WHEN 'DESC' THEN
    CASE @OrderByField
        WHEN 'CompanyName' THEN CompanyName
        WHEN 'CreatedDate' THEN CreatedDate
    END
  END DESC,
  CASE @Direction WHEN 'ASC' THEN
    CASE @OrderByField
        WHEN 'CompanyName' THEN CompanyName
        WHEN 'CreatedDate' THEN CreatedDate
    END
  END ASC

Non-matching cases will return null and so will be ignored for ordering purposes.

Sign up to request clarification or add additional context in comments.

4 Comments

Very elegant! this is exactly what i want. Two nested case statements was my approach after posting my question. You got there first!
Will be appreciated if you can help out the above problem. Thanks Bohemian!
You need to cast CreatedDate to a character type, but the syntax varies across databases: What database are you using?
i am using sql server 2013
1

As ASC and DESC are keywords, you can't have them as return value from a CASE.

You can make two cases, one for ASC and one for DESC:

SELECT *
FROM Companies
Order By 
  CASE
    WHEN @OrderByField = 'CompanyName' AND @Direction = 'ASC' THEN CompanyName
    WHEN @OrderByField = 'CreatedDate' AND @Direction = 'ASC' THEN CreatedDate
  END ASC,
  CASE
    WHEN @OrderByField = 'CompanyName' AND @Direction = 'DESC' THEN CompanyName
    WHEN @OrderByField = 'CreatedDate' AND @Direction = 'DESC' THEN CreatedDate
  END DESC

Note that all values from a case has to have the same data type, so you might need one pair of cases for string and one pair for dates.

2 Comments

My apology for not seeing your comment below. thanks for forecasting the problem that i would run into.
Question: Will your sql statement not result in something like this: "SELECT * FROM Companies Order By CompanyName ASC, DESC" ? since the second desc is outside of the case statement and will get ran no matter what.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.