| Author |
Topic |
|
duhaas
Constraint Violating Yak Guru
310 Posts |
Posted - 2007-01-05 : 14:05:03
|
| I have the following:select distinct datename(month, monthend) + ' ' + datename(year, monthend) from snapsrawproblem is results look like this:October 2006July 2006November 2006August 2006September 2006How can I make it so that the months show up in the right order |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2007-01-05 : 14:47:30
|
select distinct datename(month, monthend) + ' ' + datename(year, monthend) from snapsraworder by monthendMy bad, wasn't thinking, scratch this, see jsmith8858 below. |
 |
|
|
duhaas
Constraint Violating Yak Guru
310 Posts |
Posted - 2007-01-05 : 14:50:29
|
| yeah, tried that and i getServer: Msg 145, Level 15, State 1, Line 1ORDER BY items must appear in the select list if SELECT DISTINCT is specified.so than i tried: select distinct datename(month, monthend) + ' ' + datename(year, monthend)from snapsraworder by datename(month, monthend) + ' ' + datename(year, monthend)and its still not putting them in the right order |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-01-05 : 14:56:14
|
| Does your MonthEnd column contain only month-ending dates?If so, then just return them:select distinct monthendfrom snapsraworder by monthendand then format your monthend column at your front end. Or, if you insist on formatting data in T-SQL (never a good idea), just use GROUP BY:select monthend, datename(month, monthend) + ' ' + datename(year,monthend)from snapsrawgroup by monthendorder by monthendIf monthend has multiple dates per month other than just the ending date, then it is a little more complicated but the same basic idea.- Jeff |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2007-01-06 : 04:19:09
|
| Hi duhaas,The way to do it is you sort it using the datepart function.You can use it like thisselect blah blah order by datepart(MM,month). |
 |
|
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-01-06 : 10:36:35
|
| Nope... if you do it just by DatePart(mm,Month), and the year changes, output will not be as expected. Jeff Smith did it right.--Jeff Moden |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2007-01-07 : 01:55:53
|
| Yes but what if we sort it by both the dateparts of month as well as the year.I guess the output will be what is expected. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-07 : 05:52:33
|
With this code, it doesn't matter wether the dates are only the last date of every month. Any date is calculated to the month where they belong.select datename(month, theMonth) + ' ' + datename(year, theMonth)from ( select distinct dateadd(month, datediff(month, 0, monthend), 0) as theMonth from snapsraw ) dorder by theMonth Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-07 : 06:03:03
|
Or if you are interested in how many occurancies there are per month?select datename(month, theMonth) + ' ' + datename(year, theMonth) AS 'Long month name', theCount as 'Occurancies'from ( select dateadd(month, datediff(month, 0, monthend), 0) as theMonth, count(*) as theCount from snapsraw group by dateadd(month, datediff(month, 0, monthend), 0) ) dorder by theMonth Peter LarssonHelsingborg, Sweden |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-01-07 : 09:56:20
|
| I would first find out what kind of data is stored in the MonthEnd column. If it is just month-ending dates, or the first day of the month, or something like that (as the column name implies), it will be more efficient to just return and group/sort by the values in the column.- Jeff |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-07 : 15:02:15
|
| Yes, you are right.And it seems duhaas doesn't care since it has been two days since last checkup.Peter LarssonHelsingborg, Sweden |
 |
|
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-01-08 : 22:48:38
|
quote: Originally posted by ayamas Yes but what if we sort it by both the dateparts of month as well as the year.I guess the output will be what is expected.
Yes, but why do it with 2 calculations when 1 will do?--Jeff Moden |
 |
|
|
|