Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Old Forums
 CLOSED - General SQL Server
 Dynamic ORDER BY follow up question

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-10-01 : 09:20:12
peter writes "Loved your article on Dynamic ORDER BY, but it omitted one issue (as has every other article I've read on this subject). How does one dynamically vary the sort direction? My app implements two-way sorting on columns, so I want to be able to sort ascending or descending programmatically - in addition to specifying the sorted column programmatically. But adding DESC inside the CASE @SortField WHEN ... THEN ... ELSE NULL END construct causes a SQL error. Is there a way to get around this? I'm using SQL Server 2000.

Thanks,
Peter Cooper"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-10-01 : 09:26:17
Try this:

SELECT * FROM myTable
ORDER BY CASE @sortorder WHEN 'D' THEN sortCol ELSE Null END DESC,
CASE @sortorder WHEN 'A' THEN sortCol ELSE Null END ASC


Go to Top of Page
   

- Advertisement -