Solved

Conditional SORT BY

Posted on 2006-11-03
5
215 Views
Last Modified: 2008-02-26
I am trying to run a SELECT query from within a Stored Procedure and I need to change how I sort the SELECT depending on a variable that is passed into the SP.  Of the three fields that the SELECT can be sorted by, one is datetime, one is int, and one is nvarchar(255).  I have looked at several examples but none of them ever seem to fully work for me.  All the examples used the CASE statement.  
I have tried grouping all three WHENs under one CASE statement.  This compiles OK but when I actually run the SP it works OK for the int field and the datetime field but I get a data type conversion error on the nvarchar field.
Ive tried using three seperate CASE statements, one for each field.  I get a syntax error when I try to compile.
Ive tried nesting three CASE statements and again this compiles and runs OK on the int and datetime fields but gets a data type conversion error when sorted on the nvarchar field.
Also, I need to be able to specifiy how the sort is done, ASC v. DESC.  If I try to put ASC or DESC on the same line as the WHEN and field name I get a syntax error when I try to compile.  If I move the ASC or DESC after the END keyword, which has been suggested on a couple of examples Ive seen, I get syntax errors on the CASE statement when I try to compile.
Here's what is the most successful so far:

SELECT * FROM #TemporaryTable WHERE (RowID >= @Start) AND (RowID <= @End)
ORDER BY
      CASE @Sort
            WHEN 1 THEN empLastName
            WHEN 2 THEN chkLockedFor
            WHEN 3 THEN chkLockedDate
      END

This compiles and runs OK for @Sort = 2 & 3 but gives this error when @Sort = 1:

Syntax error converting datetime from character string.

Any ideas would be appreciated.
0
Comment
Question by:Wilbat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 17867209
you have to split like this, to avoid data type conversion issues:
SELECT * FROM #TemporaryTable WHERE (RowID >= @Start) AND (RowID <= @End)
ORDER BY CASE WHEN @Sort = 1 THEN empLastName ELSE NULL END
,CASE WHEN @Sort = 2 THEN chkLockedFor ELSE NULL END
,CASE WHEN @Sort = 3 THEN chkLockedDate ELSE NULL END

0
 
LVL 23

Expert Comment

by:adathelad
ID: 17867238
Hi Wilbat,

All 3 fields that may be ordered by must be of the same datatype, so you should convert the chkLockedFor and chkLockedDate fields to varchars:

SELECT * FROM #TemporaryTable WHERE (RowID >= @Start) AND (RowID <= @End)
ORDER BY
     CASE @Sort
          WHEN 1 THEN empLastName
          WHEN 2 THEN cast(chkLockedFor as varchar)
          WHEN 3 THEN cast(chkLockedDate as varchar)
     END



0
 

Author Comment

by:Wilbat
ID: 17867321
angelIII
that works well, but is there a way to specify ASC or DESC?

adathelad
that wont work because it kills the ability to sort correctly on the datetime field, thanks though.
0
 

Author Comment

by:Wilbat
ID: 17867334
nevermind angelIII, I figured it out.
kudos, you're the big winner!
0
 

Author Comment

by:Wilbat
ID: 17867350
for anyone that may want to know how I did the sorting, here it is:

SELECT * FROM #TemporaryTable WHERE (RowID >= @Start) AND (RowID <= @End)
ORDER BY
      CASE WHEN @Sort = 1 THEN empLastName ELSE NULL END DESC,
      CASE WHEN @Sort = 2 THEN chkLockedFor ELSE NULL END DESC,
      CASE WHEN @Sort = 3 THEN chkLockedDate ELSE NULL END ASC

just put the DESC or ASC after END.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question