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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 order by month and year

Author  Topic 

jjchan
Starting Member

5 Posts

Posted - 2008-06-02 : 06:55:26
hai freinds ,

This is the query iam using to retrieve year and its month from a datetime column

quote:
Select Distinct DateName(mm, col_datetime) as 'Month',
year(col_datetime)
as 'Year'from table_name
Order By DateName(mm, col_datetime)


result for the above query is

quote:

month year
june 2008
june 2007
july 2007
july 2008
aug 2007
aug 2008 and so on.....



but my need is

quote:

month year
june 2008
july 2008
aug 2008
june 2007
july 2007
aug 2007
and so on.....



so can someone fix it or give me a idea

thanks friends

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-02 : 07:01:58
[code]SELECT DATENAME(MONTH, col_datetime) AS [Month],
DATEPART(YEAR, col_datetime) AS [Year]
FROM table_name
GROUP BY DATENAME(MONTH, col_datetime),
DATEPART(MONTH, col_datetime),
DATEPART(YEAR, col_datetime)
ORDER BY DATEPART(YEAR, col_datetime) DESC,
DATEPART(MONTH, col_datetime)[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-02 : 07:04:57
Harsh, it doesn't work with DISTINCT keyword, unless you also have the DATEPART(MONTH, col_datetime) in the SELECT list.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

jjchan
Starting Member

5 Posts

Posted - 2008-06-02 : 08:05:48
thanks peso , it worked for me . if u could xplain how it works it would be great for me.
Go to Top of Page
   

- Advertisement -