Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to order date which month is in abbreviation format Expand / Collapse
Author
Message
Posted Saturday, February 22, 2014 8:22 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, March 6, 2014 8:24 AM
Points: 79, Visits: 106
How can I order date in ascending order in which month is in month abbreviation format. here is the data

 
Date_Month
APR 2012
JAN 2012
AUG 2012
AUG 2013
DEC 2012

I need to order it in this, in which smaller date come first
 
Date_Month
JAN 2012
APR 2012
AUG 2012
DEC 2012
AUG 2013

this is the date format function in my query
select SUBSTRING(CONVERT(VARCHAR(20),GETDATE(),113),4,8)


Post #1544247
Posted Saturday, February 22, 2014 1:55 PM This worked for the OP Answer marked as solution
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 1:08 PM
Points: 889, Visits: 5,701
SELECT	Date_Month
, LEFT(Date_Month,3) AS Mo3
, CAST(RIGHT(Date_Month,4) AS INT) AS Yr
, CAST(LEFT(Date_Month,3) + ' 1, ' + RIGHT(Date_Month,4) AS DATE) AS DateFun
FROM (
SELECT 'APR 2012' AS Date_Month
UNION ALL
SELECT 'JAN 2012'
UNION ALL
SELECT 'AUG 2012'
UNION ALL
SELECT 'AUG 2013'
UNION ALL
SELECT 'DEC 2012') x
ORDER BY DateFun DESC;

Post #1544260
Posted Sunday, February 23, 2014 5:52 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, November 24, 2014 4:53 AM
Points: 3,422, Visits: 5,368
Why not just do it like this?

WITH SampleData (Date_Month) AS
(
SELECT 'APR 2012'
UNION ALL SELECT 'JAN 2012'
UNION ALL SELECT 'AUG 2012'
UNION ALL SELECT 'AUG 2013'
UNION ALL SELECT 'DEC 2012'
)
SELECT Date_Month
FROM SampleData
ORDER BY CAST(Date_Month AS DATE);





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1544333
Posted Monday, February 24, 2014 3:19 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 1:10 PM
Points: 260, Visits: 1,088
Your solution reminded me of this SQL Spackle article from Jeff Moden.

http://www.sqlservercentral.com/articles/T-SQL/71511/
Post #1544731
Posted Monday, February 24, 2014 5:20 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, May 5, 2014 6:31 AM
Points: 288, Visits: 519
create a datetime dimension and join to it for the sort

Create Table dimDateTime (
DateOrder int,
MonthDescr varchar(10)
)
insert into dimDateTime values
(1,'JAN 2012'),
(2,'FEB 2012'), etc

select stuff
from MyTable
join dimDateTime
on dateColumn = MonthDescr
order by DateOrder
Post #1544744
Posted Monday, February 24, 2014 5:44 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, November 24, 2014 4:53 AM
Points: 3,422, Visits: 5,368
AronB (2/24/2014)
Your solution reminded me of this SQL Spackle article from Jeff Moden.

http://www.sqlservercentral.com/articles/T-SQL/71511/


It could very well be that that is where I remembered this from.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1544746
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse