May 24, 2011 at 2:15 am
Hello,
I have a requirement where I need to sort the months in chronological sequence. This is actually a parameter where the months would be displayed in the drop down list. I have tried doing this thru the following code but it does not get the desired result:
-------------------------------------------------------------------------------
SELECT SUBSTRING(CONVERT(VARCHAR(11), create_dt, 113), 4, 8) AS create_dt
FROM mf_forum
where create_dt >= '01-10-2010'
GROUP BY SUBSTRING(CONVERT(VARCHAR(11), create_dt, 113), 4, 8)
ORDER BY datepart(mm,SUBSTRING(CONVERT(VARCHAR(11), create_dt, 113), 4, 8))
--------------------------------------------------------------------------------
The result that I get is:
Jan 2011
Feb 2011
Mar 2011
Apr 2011
May 2011
Oct 2010
Nov 2010
Dec 2010
-----------------------------------------
But this is what I want to achieve:
Oct 2010
Nov 2010
Dec 2010
Jan 2011
Feb 2011
Mar 2011
Apr 2011
May 2011
-----------------------------------------------
Could someone please tell me what I need to change in the code to achieve the above result ?
Thanks and Regards,
Paul
May 24, 2011 at 12:29 pm
ORDER BY CAST(SUBSTRING(CONVERT(VARCHAR(11), date, 113), 4, 8) AS DATETIME)
May 25, 2011 at 2:27 am
May 25, 2011 at 2:35 am
nigel. (5/25/2011)
Wouldn't it just be:ORDER BY create_dt
No, and I've just realised why, sorry :blush:
Too early, need more coffee
May 25, 2011 at 2:49 am
LutzM (5/24/2011)
ORDER BY CAST(SUBSTRING(CONVERT(VARCHAR(11), date, 113), 4, 8) AS DATETIME)
You are the Man, Lutz !!!!! Genius !!!
Thanks very much !!!
May 25, 2011 at 3:01 am
LutzM (5/24/2011)
ORDER BY CAST(SUBSTRING(CONVERT(VARCHAR(11), date, 113), 4, 8) AS DATETIME)
Hi Lutz,
I was stuck with another issue and your solution resolved that one as well. You hit two birds with one stone !!!! Thanks a lot again !!!.
This was the initial problem:
http://www.sqlservercentral.com/Forums/Topic1113899-149-1.aspx#bm1114034
Regards,
Paul
May 25, 2011 at 12:20 pm
May 25, 2011 at 3:28 pm
When I can't port the datetime column in the report I simply do group and order by YEAR, then MONTH. But I guess that concatenating that into a single column should make for a lighter load on SSRS! Maybe I'll test someday ;-).
May 26, 2011 at 9:53 am
LutzM (5/25/2011)
Glad I could help 😀
Could I please ask you for another favor ? I am stuck with this one:
http://www.sqlservercentral.com/Forums/Topic1115231-149-1.aspx
Lutz, Your help would be deeply appreciated.
Thanks and Regards,
Paul
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply