Hello,
After a little search and testing i made one solution for you .Try this one
First I create one temporary table
create table #tmp
(
dr varchar(50)
)
Now insert records
insert into #tmp values ('2015_feb')
insert into #tmp values ('2015_june')
insert into #tmp values ('2015_jan')
insert into #tmp values ('2016_May')
insert into #tmp values ('2014_feb')
According to the requirement , the order will be Year and month wise
That's why First substring the value to get Year part and month part separately .
as the year length is 4 hence substring it upto 4 digit to get the Year value and
for month part as we are getting value by MonthName i.e , May or June or July
so , just convert the Month name to corresponding Value means
Jan-->1
Feb-->2
Mar-->2
Apr-->4
so on
For that issue , here I simply use this format ('2016-'+MonthValue + '-01 10:12:20.123') to generate a new datetime as I have to pass one
datetime value in another query to get the Month value of corresponding month
Here month value like 5 for May or 6 for June or 1 for Jan .
To get the month value from corresponding Month use below query
select datepart(mm, convert(datetime,Getdate()))
Here OP will be 5 as the current month is May
Ultimate query will be
select * from #tmp ORDER BY YEAR(substring(dr,1,4)),
DATEPART(MM,(select convert(varchar(20),Convert(datetime,'2016-'+convert(varchar,substring(dr,6,len(dr)-5))+'-01 10:12:20.123'),107))) asc
and OP will be
dr
2014_feb
2015_jan
2015_feb
2015_june
2016_may
Thanks