troubleshooting Question

Sorting ASC DESC using CASE

Avatar of champ_010
champ_010 asked on
Microsoft SQL Server
3 Comments1 Solution2336 ViewsLast Modified:
I would like to sort a table depending on a value I pass to the stored procedure. This is what I have:

CREATE PROCEDURE [dbo].[GetVFMessages] (@topicID int,@sortBy bit) AS
SELECT tblVFMessages. *, vfTopicName FROM tblVFMessages
  INNER JOIN tblVFTopics ON tblVFMessages.vfTopicID = tblVFTopics.vfTopicID
  WHERE tblVFMessages.vfTopicID=@topicID
ORDER BY
   CASE
     WHEN @sortBy =' ' THEN vfPostDate DESC
   END,
   CASE
     WHEN @sortBY='0' THEN vfPostDate DESC
   END,
   CASE
     WHEN @sortBy='1' THEN vfPostDate ASC
   END;
GO

The DESC and ASC that I put in the different CASEs run an error.  It works if I remove them.  How can I sort the column vfPostDate by the value I pass in?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros