SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Dynamic Order by -need both Dates and Varchars


Dynamic Order by -need both Dates and Varchars

Author
Message
bboufford
bboufford
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 Visits: 45
I am using a dynamic order by in a SSRS Report

(note this is not a working query for example only)
Select No.
Description
Title
Author
Published Date
Mfg Date
from all of my tables
order by Order by Case @orderby when 'Job' then Job.[No_]
when 'Author' then Job.[Description 2]
WHEN 'MSDue' then CASE Job.[MFG Date] WHEN '1753-01-01 00:00:00.000' Then '12-31-2049'
ELSE Job.[MS Due Date]
END

I know I am supposed to use only Text or Dates but when I convert my dates to text they don't sort correctly. Can anyone help me with this conundrum. Thank you

Barb
LutzM
LutzM
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38145 Visits: 13559
You need to convert your date values to varchar using CONVERT() function.
You could use CONVERT(VARCHAR(19),Job.[MS Due Date],120) to get a date format which can be sorted.



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
bboufford
bboufford
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 Visits: 45
I do not get any sorting happening at all when I use your suggestion - any more suggestions
;-) Thanks


WHEN 'MSDue' then CASE CONVERT(VARCHAR(19),Job.[MS Due Date],120) WHEN '1753-01-01 00:00:00.000' Then '12-31-2049'
ELSE CONVERT(VARCHAR(19),Job.[MS Due Date],120)
End
LutzM
LutzM
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38145 Visits: 13559
No. Not in this thread, since I don't think there should be a parallel discussion.



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
bboufford
bboufford
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 Visits: 45
So how do I get it out of here
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search