Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using date name with group by

Status
Not open for further replies.

gtjr92

Programmer
May 26, 2004
96
I have a stored procedure that gets the month name and the number of items for that month from a table.
I get all that fine however I cannot get it to orderby or
group by the date name properly.
it sorts it by the actually name instead of the order of the month. ie july is sorted before june
My results look like this
month year count
July 2005 3
June 2005 1
September 2005 1

Code:
Select  distinct datename(Month,DateCreated)As Month,
datepart(year,DateCreated) As Year,count(*) As EntryCount  
 from blog_entry  
WHERE blog_Entry.IsDeleted = 0 And blog_Entry.entBlTopicid='1'
group by datecreated
 
I just added this to my query and orderby by it and it worked, it ehre a better way of doing it?
Code:
datepart(Month,DateCreated)As Monthnum
order by monthnum
 
That would be the way to do it.

You have to put the datepart command in the group by statement otherwise it won't work correctly.

Denny
MCSA (2003) / MCDBA (SQL 2000) / MCTS (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top