Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8188001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:45:23+00:00 2026-06-07T02:45:23+00:00

I am passing two parameters: @Column and @Direction, both of them are nvarchar(50) I

  • 0

I am passing two parameters:

@Column and @Direction, both of them are nvarchar(50)

I want to change the order by of the select query from the values inside the parameters.

For example,

if @Column = Name and @Direction = 'Desc' then order by Name desc
if @Column = Name and @Direction = 'Asc'  then order by Name asc
if @Column = Type and @Direction = 'Desc' then order by Type desc
if @Column = Type and @Direction = 'Asc'  then order by Type asc

My table is called Cars.

it has three columns: CarId, Name, Type

I tried to do this but it doesn’t return the right result (as if order by is ignored)

select * from Cars
order by        
case 
when @SortColumn = 'Type' and @SortDir = 'Desc' then 1
when @SortColumn = 'Type' and @SortDir = 'Desc' then 1
when @SortColumn = 'Name' and @SortDir = 'Asc'  then 3
when @SortColumn = 'Name' and @SortDir = 'Asc'  then 3, end asc
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:45:25+00:00Added an answer on June 7, 2026 at 2:45 am

    CASE is an expression that yields a single value. It can’t be used for control of flow like you might do in other languages or you might try with IF. You can’t use IF within a query either.

    The following treats each condition separately. If the conditions don’t match, the result for that expression is NULL, which is ignored in any ordering. So the order of these clauses is irrelevant, since only the ones where all conditions are true will be observed:

    ORDER BY 
      CASE WHEN @SortColumn = 'Type' AND @SortDir = 'DESC' THEN Type END DESC,
      CASE WHEN @SortColumn = 'Name' AND @SortDir = 'DESC' THEN Name END DESC,
      CASE WHEN @SortColumn = 'Type' AND @SortDir = 'ASC'  THEN Type END,
      CASE WHEN @SortColumn = 'Name' AND @SortDir = 'ASC'  THEN Name END;
    

    These can probably be combined somewhat, but I don’t know the data types, so this is probably safest. If the data types are the same, then:

    ORDER BY 
      CASE WHEN @SortDir = 'DESC' THEN 
        CASE WHEN @SortColumn = 'Type' THEN Type ELSE Name END 
      END DESC,
      CASE WHEN @SortDir = 'ASC' THEN
        CASE WHEN @SortColumn = 'Type' THEN Type ELSE Name END
      END;
    

    You can’t collapse much further because you can’t do conditional ASC vs. DESC in one expression. But you can simplify in other ways, e.g. the following queries will yield the same results as above, even though in some cases the order by will attempt to do (say) ORDER BY Type DESC, Type DESC – I’m not 100% sure if the optimizer simplifies that as redundant but it should collapse to a single sort operator (when required – sometimes this query could yield an inherent sort based on the index used to satisfy the query).

    ORDER BY 
      CASE WHEN @SortColumn = 'Type' AND @SortDir = 'DESC' THEN Type END DESC,
      CASE WHEN @SortColumn = 'Name' AND @SortDir = 'DESC' THEN Name END DESC,
      CASE WHEN @SortColumn = 'Type' THEN Type END,
      CASE WHEN @SortColumn = 'Name' THEN Name END;
    

    …or…

    ORDER BY 
      CASE WHEN @SortDir = 'DESC' THEN 
        CASE WHEN @SortColumn = 'Type' THEN Type ELSE Name END 
      END DESC,
      CASE WHEN @SortColumn = 'Type' THEN Type ELSE Name END;
    

    You could also consider dynamic SQL since, if this gets much more complex, there is a lot of maintenance here.

    DECLARE @sql NVARCHAR(MAX);
    SET @sql = N'SELECT ... ORDER BY ' + @SortColumn + ' ' + @SortDir;
    PRINT @sql;
    -- EXEC sp_executesql @sql;
    

    Of course this can introduce other issues (maintainability of the rest of the query, plan cache bloat…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm passing parameters to a server from a Flash Builder application. I want to
I am passing two parameters i.e @AccountId and @key . I want to show
i am passing two strings from a c file to asm file as parameters.
I'm trying to glue together two web services by passing a value from one
I have an IF statement that consists of two separate function calls passing values
Possible Duplicate: Passing two command parameters using a WPF binding I need that send
i have problem with two parameters passing with URL link. Can anyone help me?
I have a stored proc that I am passing two parameters into. One parameter
I have a report that takes two parameters from a couple of text boxes
I have read about passing parameters from jsf page to managedbean through actionListener. Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.