Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have data in a column in below manner
2013-Jan
2013-Oct
2012-Jan
2012-Oct
2012-April
2013-April

.......So on in the irrespective order prolong descending ten years.

I need a query which generates the output as below

2013-Oct
2013-April
2013-Jan
2012-Oct
2012-April
2012-Jan
.....
...... in descending manner.


Please Help me out

Thanks,
Vinay
Posted
Comments
Sergey Alexandrovich Kryukov 9-Feb-13 0:49am    
What did you try?
—SA
Sergey Alexandrovich Kryukov 9-Feb-13 0:50am    
Please stop commenting anything as "solution", this is the abuse. Comment on any existing post, reply on existing comments, use "Improve question".
—SA
DaveAuld 9-Feb-13 1:46am    
And what data format is the datatype in the column, I am assuming it is string(/nvarchar) rather than DateTime that has just been formatted.
Richard MacCutchan 9-Feb-13 5:20am    
Are these columns text or dates? If they are proper DateTime types then you just order them in descending order.

1 solution

I think you need something like this:
SQL
declare @table table
(
    DateStr varchar(10)
)

insert into @table values('2013-Jan')
insert into @table values('2013-Oct')
insert into @table values('2012-Jan')
insert into @table values('2012-Oct')
insert into @table values('2012-April')
insert into @table values('2013-April')

select * from @table
order by CONVERT(date, DateStr+'-01') DESC
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900