Home All Groups Group Topic Archive Search About

ORDER BY @OrderBy problem

Author
9 Dec 2008 4:40 PM
ThatsIT.net.au
I get this error from the following code as last modified is a date., I can
cast it as varchar but then it sorts as text not date. any ideas

Conversion failed when converting datetime from character string


CASE @Direction

WHEN 'DESC' THEN

CASE @OrderBy


WHEN 'FirstName' THEN firstname

WHEN 'TelNo' THEN TelNo

WHEN 'Email' THEN email

WHEN 'FaxNo' THEN FaxNo

WHEN 'LastModified' THEN  LastModified


                                 END

END

  DESC,

CASE @Direction

WHEN 'ASC' THEN

  CASE @OrderBy



WHEN 'FirstName' THEN firstname

  WHEN 'TelNo' THEN TelNo

WHEN 'Email' THEN email

WHEN 'FaxNo' THEN FaxNo

WHEN 'LastModified' THEN  LastModified

END

END

Author
9 Dec 2008 4:54 PM
Russell Fields
Since the CASE only returns one column, then it can only return one
datatype.  And the datatype is determined by the datatype precedence.
http://msdn.microsoft.com/en-us/library/ms190309(SQL.90).aspx

Looking at that shows that, by default, character strings will be cast into
datetime instead of datetime being cast into a character string.  Although
character strings (char, nchar, varchar, nvarchar) may seem more general,
they are very low in precedence.

What you can do in this case is:

WHEN 'LastModified' THEN  CONVERT (VARCHAR(23), LastModified, 121)

RLF




Show quoteHide quote
"ThatsIT.net.au" <me@work> wrote in message
news:9283937A-5F49-4182-B3FD-8865087DCE02@microsoft.com...
>I get this error from the following code as last modified is a date., I can
>cast it as varchar but then it sorts as text not date. any ideas
>
> Conversion failed when converting datetime from character string
>
>
> CASE @Direction
>
> WHEN 'DESC' THEN
>
> CASE @OrderBy
>
>
> WHEN 'FirstName' THEN firstname
>
> WHEN 'TelNo' THEN TelNo
>
> WHEN 'Email' THEN email
>
> WHEN 'FaxNo' THEN FaxNo
>
> WHEN 'LastModified' THEN  LastModified
>
>
> END
>
> END
>
>  DESC,
>
> CASE @Direction
>
> WHEN 'ASC' THEN
>
>  CASE @OrderBy
>
>
>
> WHEN 'FirstName' THEN firstname
>
>  WHEN 'TelNo' THEN TelNo
>
> WHEN 'Email' THEN email
>
> WHEN 'FaxNo' THEN FaxNo
>
> WHEN 'LastModified' THEN  LastModified
>
> END
>
> END
>
>
>
>
>
>
>
>
>
>
>
Are all your drivers up to date? click for free checkup

Author
9 Dec 2008 6:47 PM
Plamen Ratchev
As alternative to casting DATETIME to character data type, you can have
additional CASE expressions for each data type.

ORDER BY CASE @Direction
           WHEN 'DESC'
           THEN
             CASE @OrderBy
               WHEN 'FirstName' THEN firstname
               WHEN 'TelNo' THEN TelNo
               WHEN 'Email' THEN email
               WHEN 'FaxNo' THEN FaxNo
             END
          END DESC,
          CASE @Direction
           WHEN 'DESC'
           THEN
             CASE @OrderBy
               WHEN 'LastModified' THEN  LastModified
             END
          END DESC,
          CASE @Direction
           WHEN 'ASC'
           THEN
             CASE @OrderBy
               WHEN 'FirstName' THEN firstname
               WHEN 'TelNo' THEN TelNo
               WHEN 'Email' THEN email
               WHEN 'FaxNo' THEN FaxNo
             END
          END ASC,
          CASE @Direction
           WHEN 'ASC'
           THEN
             CASE @OrderBy
               WHEN 'LastModified' THEN  LastModified
             END
          END ASC;

You can also try dynamic SQL to see if that will perform better:
http://www.sommarskog.se/dynamic_sql.html#Order_by

--
Plamen Ratchev
http://www.SQLStudio.com
Author
9 Dec 2008 6:57 PM
Gareth Erskine-Jones
On Wed, 10 Dec 2008 01:40:35 +0900, "ThatsIT.net.au" <me@work> wrote:

>I get this error from the following code as last modified is a date., I can
>cast it as varchar but then it sorts as text not date. any ideas
>
>Conversion failed when converting datetime from character string
>
>
>CASE @Direction
>
> WHEN 'DESC' THEN
>
> CASE @OrderBy

each potential value returned from your CASE statement must be of the
same type, so you do need to cast LastModified to a varchar. This
shouldn't be a problem - you can specify the format the datetime
should be in, and if you use a format like yyyy-mm-dd hh:mm, then
sorting by the varchar should give you the same results as sorting on
the date.

The 4 guys have a good article about this:
http://www.4guysfromrolla.com/webtech/010704-1.shtml


Bookmark and Share