Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
SQLNoob81
Starting Member
38 Posts |
Posted - 2011-01-10 : 18:42:29
|
Hi GuysI gave the following select statement:SELECT DateName(month,dates) AS MyMonth,COUNT(*) AS InterviewsFROM Roomswhere YEAR(dates) = 2010group by DATENAME(month,dates)order by MyMonth which returns the following:MyMonth Interviews April 774August 773December 245February 684January 604July 836June 780March 1070May 745November 978October 873September 708How can I order by month "value" and not alphabetical value of month, Example:MyMonth Interviews January 604February 684March 1070April 774May 745June 780July 836August 773September 708October 873November 978December 245Thanks |
|
|
malpashaa
Constraint Violating Yak Guru
264 Posts |
Posted - 2011-01-10 : 19:13:50
|
Try something like this:DECLARE @year_number INT;DECLARE @year DATETIME;DECLARE @next_year DATETIME;SET @year_number = 2010;SET @year = CAST(@year_number AS CHAR(4)) + '0101';SET @next_year = DATEADD(YEAR, 1, @year);SELECT DATENAME(MONTH, dates) AS MyMonth, COUNT(*) AS Interviews FROM (SELECT DATEADD(MONTH, DATEDIFF(MONTH, @year, dates), @year) AS dates FROM Rooms WHERE dates >= @year AND dates < @next_year) AS R GROUP BY dates ORDER BY dates; |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-01-10 : 20:58:13
|
You could just do:SELECT DateName(month,dates) AS MyMonth,COUNT(*) AS InterviewsFROM Roomswhere YEAR(dates) = 2010group by DATENAME(month,dates)order by DatePart(month,dates) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-01-11 : 01:13:50
|
[code]SELECT DateName(month, dates) AS MyMonth,COUNT(*) AS InterviewsFROM dbo.Roomswhere dates >= '20100101' and dates < '20110101'group by DATENAME(month, dates), datepart(month, dates)order by datepart(month, dates)[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-01-11 : 01:24:40
|
| select DATENAME(month,dates) as MyMonth,COUNT(1) as Interviews from dbo.Roomswhere YEAR(dates)=2010Group by DATENAME(month,dates),MONTH(dates)order by MONTH(dates)Cheers!MIK |
 |
|
|
SQLNoob81
Starting Member
38 Posts |
Posted - 2011-01-11 : 05:02:18
|
| Thanks a Mil Mik_2008, that worked!!!! |
 |
|
|
|
|
|
|
|