如何按字母C#ASPX对月份名称进行排序

时间:2018-01-30 06:43:59

标签: c# asp.net charts

目前,我的输出是

Mar, Jan, Feb

但我希望它是Jan, Feb, Mar

这是我的代码,任何人都可以帮助我获得分拣月份输出。

using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("Select distinct Count(Feedback_id) as CountFeedback, CONVERT(varchar(3), visit_date, 100) as Month " +
                    "FROM Patient_Feedback " +
                    "WHERE YEAR(visit_date) = " + drpValue + " and feedback_type = 'appeal' " +
                    "Group by CONVERT(varchar(3), visit_date, 100) " +
                    "ORDER BY Month desc", con);
                Series series = Chart2.Series["Series2"];
                Chart2.ChartAreas["ChartArea2"].AxisX.Interval = 1;
                con.Open();

                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    series.Points.AddXY(rdr["Month"].ToString(), rdr["CountFeedback"].ToString());
                }
                string impPath = HttpContext.Current.Request.PhysicalApplicationPath + tmpChartName;
                Chart2.SaveImage(impPath);
            }

2 个答案:

答案 0 :(得分:0)

替换

CONVERT(varchar(3), visit_date, 100) as Month

使用

DATEPART(m, visit_date) as Month

你的查询应该是这样的(我没有尝试编译它)

Select distinct Count(Feedback_id) as CountFeedback, CONVERT(varchar(3), visit_date, 100) as Month, DATEPART(m, visit_date) MonthNumber
FROM Patient_Feedback
WHERE YEAR(visit_date) = drpValue and feedback_type = 'appeal'
Group by CONVERT(varchar(3), visit_date, 100), DATEPART(m, visit_date)
ORDER BY DATEPART(m, visit_date)

答案 1 :(得分:0)

如果您只想按升序排序日期,为什么不按日期排序asc,如下所示:

SqlCommand cmd = new SqlCommand("Select distinct Count(Feedback_id) as CountFeedback, CONVERT(varchar(3), visit_date, 100) as Month " +
            "FROM Patient_Feedback " +
            "WHERE YEAR(visit_date) = " + drpValue + " and feedback_type = 'appeal' " +
            "Group by CONVERT(varchar(3), visit_date, 100), visit_date " +
            "ORDER BY visit_date ASC", con);