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


How to order date which month is in abbreviation format


How to order date which month is in abbreviation format

Author
Message
sqlbaby2
sqlbaby2
Old Hand
Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)

Group: General Forum Members
Points: 311 Visits: 256
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)


pietlinden
pietlinden
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13464 Visits: 14097
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;


dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17077 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
AronB
AronB
Old Hand
Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)

Group: General Forum Members
Points: 353 Visits: 1329
Your solution reminded me of this SQL Spackle article from Jeff Moden.

http://www.sqlservercentral.com/articles/T-SQL/71511/
EricEyster
EricEyster
Right there with Babe
Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)

Group: General Forum Members
Points: 718 Visits: 520
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
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17077 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
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