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 |
|
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_nameOrder By DateName(mm, col_datetime)
result for the above query is quote: month yearjune 2008june 2007july 2007july 2008aug 2007aug 2008 and so on.....
but my need is quote: month yearjune 2008july 2008aug 2008june 2007july 2007aug 2007 and so on.....
so can someone fix it or give me a ideathanks 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_nameGROUP 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" |
 |
|
|
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" |
 |
|
|
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. |
 |
|
|
|
|
|
|
|