Sunday, September 9, 2007

ordering character year and month

-- If you have data in character format as below
-- and would like to sort on year and month

create table #t (c varchar(50))

insert into #t values ('Apr 2007')
insert into #t values ('June 2008')
insert into #t values ('Sep 2007')
insert into #t values ('Nov 2007')
insert into #t values ('Feb 2006')
select * from #t
order by convert(datetime,c,103) asc
drop table #t
go

No comments: