Something like below; not sure if you wanted a sum() or what for the number:
select sum(number) as number_total,
dateadd(month, datediff(month, 0, date_created), 0) as month_created
from dbo.student
where date_created >= '20160101'
and date_created < '20161001'
group by dateadd(month, datediff(month, 0, date_created), 0)
select sum(number) as number_total, stuff(convert(varchar(11),
(dateadd(month, datediff(month, 0, date_created), 0), 0), 4, 6, '-')
as month_created
from dbo.student
where date_created >= '20160101'
and date_created < '20161001'
group by dateadd(month, datediff(month, 0, date_created), 0)
Thanks so much, Scott. Sadly this isn't working for me. I get an error "Incorrect syntax near ','.", is this because my date format is different to yours? Please note my date format is ''2016-02-01''.
If the data type is date or datetime, there are no dashes in the actual data, that's just for display. It's safest to always enter dates as yyyymmdd, with no punctuation, so that the date cannot possibly be misinterpreted.
select sum(number) as number_total, stuff(convert(varchar(11),
dateadd(month, datediff(month, 0, date_created), 0), 0), 4, 6, '-')
as month_created
from dbo.student
where date_created >= '20160101'
and date_created < '20161001'
group by dateadd(month, datediff(month, 0, date_created), 0)
This is interesting, thanks Jeff! The problem now is that I have multiple for each month. Using the below script, it seems that I am not grouping by the month but by the day I guess?
select convert(char(7),
date_created, 121)
as month_created, count(number) as number_total
from dbo.student
where date_created >= '20160101'
and date_created < '20161001'
group by date_created
select convert(char(7), date_created, 121) as month_created,
count(number) as number_total
from dbo.student
where date_created >= '20160101'
and date_created < '20161001'
group by convert(char(7), date_created, 121);