SQL Server Performance

Invalid sorting date column in .net

Discussion in 'General Developer Questions' started by ali_ggl, Jun 11, 2008.

  1. ali_ggl New Member

    =======================================================================
    Date sorting is not valid in data grid using asp.net.
    -------------------------------------------------------

    Hi i am using MS SQL Server 2000 on back end and on front end i am using ASP.NET to show the data while accessing data from table i want to sort date in descending order but it shows below invalid order.

    4/10/2008
    4/10/2008
    4/10/2008
    4/15/2008
    4/15/2008
    4/9/2008
    4/9/2008
    4/9/2008
    9/10/2007


    the exact order is

    4/9/2008
    4/9/2008
    4/9/2008
    4/10/2008
    4/10/2008
    4/10/2008
    4/15/2008
    4/15/2008
    9/10/2007

    which is not coming in front so if any one have idea plz guide me
    Regards.
    Ali.
  2. ali_ggl New Member

    Even i used below code in c# to sort my data table before its display but still sorting is not valid.
    Code:
    C#
    //datatable is populated before all the below lines
    //we can assign datatable as DataScource to data grid and bind also
    //but the result will be the same as i discussed in this pose
    DataView dv = datatable.DefaultView;
    dv.Sort = "TaskDate";
    // "TaskDate" is column of datatable
    this.dgTaskReport.DataSource = dv;
    this.dgTaskReport.DataBind();
    //dgTaskReport is data grid name
    
  3. SQL2000DBA New Member

    Hi,
    You can add order by clause in your query. I am getting data without order by clause as below
    06/11/08
    06/10/08
    06/09/08
    06/08/08
    06/07/08
    06/06/08
    04/22/08
    03/23/08
    After adding below order by clause in my query. I am getting data as per your requirement.
    06/06/08
    06/07/08
    06/08/08
    06/09/08
    06/10/08
    06/11/08
    04/22/08
    03/23/08
    order by clause: ORDER BY DATEPART(DD,<datecolumnname>)
  4. atulmar New Member

    By Looking at the sorted data, I think the date column is considered as character column.
    Can you try to cast it to datetime and then ordering by that.
  5. Madhivanan Moderator

    [quote user="atulmar"]
    By Looking at the sorted data, I think the date column is considered as character column.
    Can you try to cast it to datetime and then ordering by that.
    [/quote]
    Thats what my guess too
    It should be in DATETIME column to have proper ordering

Share This Page