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
 General SQL Server Forums
 New to SQL Server Programming
 Sorting the column values

Author  Topic 

dim
Yak Posting Veteran

57 Posts

Posted - 2010-11-26 : 14:11:02
Hi,

I have a database column Month defined as varvhar coming out as random months. What I need to do is for each year the month column lays out data in the sacending order of the months.

The systax are as follows:

SELECT
RTRIM(dbo.vw2_Dim_Date.Month_Name) + ' ' + cast(ltrim (dbo.vw2_Dim_Date.Calendar_Year) as varchar) as Month_Name,
dbo.vw2_Dim_Date.Month_Name as Month_Name_Date_Dim,
dbo.vw2_Dim_Date.Calendar_Year as Calender_Year,
AVG(cast(b.Shift as float)/60) AS Shift_Average,
FROM a,
b,
c,
dbo.vw2_Dim_Date
WHERE
b.F_Key = a.F_Key
and b.Key = a.Key
and b.y = a.y
and c.y = a.y
--and dbo.vw2_Dim_Date.Date between '04/30/2010' and '06/01/2010'
and a.date_key = dbo.vw2_Dim_Date.date_key
group by
RTRIM(dbo.vw2_Dim_Date.Month_Name) + ' ' + cast(ltrim (dbo.vw2_Dim_Date.Calendar_Year) as varchar),
dbo.vw2_Dim_Date.Month_Name,
dbo.vw2_Dim_Date.Calendar_Year

Please let me know of any ways to have the month name column as ascending order sort like Janurary, February, March.....etc..

Thank You,
dim


Dp

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2010-11-26 : 14:14:37
Add ORDER BY MONTH(dbo.vw2_Dim_Date.Date) ASC at the bottom of the query




Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-27 : 01:38:04
if you've multiple year also coming make sure you add YEAR(Date) also in ORDER BY

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-29 : 05:19:36
1 Do the formation at front end application
2 Use unambigiuos date formats
http://beyondrelational.com/blogs/madhivanan/archive/2010/06/03/understanding-datetime-column-part-ii.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -