how to sort record by date

Last post 02-22-2009 4:27 AM by mudassarkhan. 9 replies.

Sort Posts:

  • how to sort record by date

    02-19-2009, 11:48 PM

    Hi friend,

    I would like to sort my records by Date and fill then in datagrid. while adding them i am converting the date format to  System.DateTime.Now.ToString("dd/MM/yyyy HH:mm");

    but when i am selecting the record with this query

    strquery = "select * from Clients order by date desc"  the records r sorted by date but notby month , becoze the date is fiest and monthe is second and year is third in the Sql server data base.

    how can i get the record sorted by month. I want the latest record first

    PLEASE MARK AS ANSWER IF MY POST SOLVED YOUR PROBLEM
    Filed under: ,
  • Re: how to sort record by date

    02-20-2009, 4:44 AM
    • Contributor
      2,488 point Contributor
    • santosh_maharaja
    • Member since 01-19-2009, 11:41 AM
    • Pune, India
    • Posts 418

    Try out this.

    Make order by Convert(Datetime, date)

    Regards,
    santosh_maharaja

    Please mark as answer if you got expected solution.
  • Re: how to sort record by date

    02-22-2009, 3:40 AM

     

    I have a column in the database 'date' and the values i am stoting in it as 'dd/mm/yyyy hh/mm'

    i want the record in order by date column, order should be in month value

    PLEASE MARK AS ANSWER IF MY POST SOLVED YOUR PROBLEM
  • Re: how to sort record by date

    02-22-2009, 3:45 AM
    • All-Star
      59,913 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    indianmaestro:

    Hi friend,

    I would like to sort my records by Date and fill then in datagrid. while adding them i am converting the date format to  System.DateTime.Now.ToString("dd/MM/yyyy HH:mm");

    but when i am selecting the record with this query

    strquery = "select * from Clients order by date desc"  the records r sorted by date but notby month , becoze the date is fiest and monthe is second and year is third in the Sql server data base.

    how can i get the record sorted by month. I want the latest record first

    To Sort by month use this query

    select * from Clients order by datepart(month, date) desc

  • Re: how to sort record by date

    02-22-2009, 3:57 AM

    the column name id date but the type is 'varchar' so i ma getting an error

    The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

    PLEASE MARK AS ANSWER IF MY POST SOLVED YOUR PROBLEM
  • Re: how to sort record by date

    02-22-2009, 4:05 AM
    • All-Star
      59,913 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    indianmaestro:

    the column name id date but the type is 'varchar' so i ma getting an error

    The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

    It should be a DateTime datatype So that it can work Since it is not then do this conversion Note it will only work if the date is valid

    select * from Clients order by datepart(month, Convert(datetime, Date)) desc

  • Re: how to sort record by date

    02-22-2009, 4:10 AM

    i got this error The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

    PLEASE MARK AS ANSWER IF MY POST SOLVED YOUR PROBLEM
  • Re: how to sort record by date

    02-22-2009, 4:13 AM
    • All-Star
      59,913 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    indianmaestro:
    i got this error The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

     

    what is the name of the column which has date stored??

  • Re: how to sort record by date

    02-22-2009, 4:20 AM

    the name in the database column is date.

    when i opened in designtable i noticed that there [ ,] to the column name

    PLEASE MARK AS ANSWER IF MY POST SOLVED YOUR PROBLEM
  • Re: how to sort record by date

    02-22-2009, 4:27 AM
    Answer
    • All-Star
      59,913 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    indianmaestro:

    the name in the database column is date.

    when i opened in designtable i noticed that there [ ,] to the column name

    date is a keyword hence chnage the name to [date] or give some other name

Page 1 of 1 (10 items)