sql server - T-SQL Sorting by year and month -


i sort date according date yyyy,mm. tried using varchars first:

group (year(join_date) varchar(4)) + '-' + cast(month(join_date) varchar(2)) 

but converting them varchar meant sort went 2014.1 - 2014.11 - 2014.12 - 2014.2 how sort them in year month order?

select year(join_date) + month(join_date) date, count(*) count x mtype ='a' , (join_date between dateadd(year, -5, getdate()) , getdate()) group year(join_date) + month(join_date) order date asc 

you don't need convert them varchar, year , month return ints:

select year(join_date) joinedyear, month(join_date) joinedmonth, count(*) count x mtype ='a' , (join_date between dateadd(year, -5, getdate()) , getdate()) group year(join_date), month(join_date) order year(join_date) asc, month(join_date) asc 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -