sort by column and date

  • I have few columns in my table. But i want to sort the data by two columns. My data looks like below-

    A9/23/2014
    ANULL
    A9/23/2015
    A9/29/2016
    B10/1/2018
    B9/29/2016
    C10/29/2017
    CNULL

    i want to sort by column 1st and then column 2nd which is date as. showing the NULL date first within the column 1st. for eg wherever the value is "A" it should sort by NULL date first then from old to new date in ascending.

    ANull
    A9/23/2014
    A9/23/2015
    A9/29/2016
    B9/29/2016
    B10/1/2018
    CNULL
    C10/29/2017
  • SELECT col1, col2
    FROM Yourtable
    ORDER BY col1, col2

    That should be sufficient, as ORDER BY puts NULL values ahead of any other values.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • SELECT * FROM TABLE_NAME
    ORDER BY 1,2

    SELECT * FROM TABLE_NAME
    ORDER BY FISRTCOLUMN, SECONDCOLUMN

    ***The first step is always the hardest *******

  • Both of these query not giving me correct result when there are NULLs.

  • Papil - Wednesday, June 20, 2018 9:43 AM

    Both of these query not giving me correct result when there are NULLs.

    Show me the query you are actually using.  NULLS always sort first in an ASC (ascending) sort, which is the default when you don't specify a sort order for a given column in the ORDER BY clause.   Something isn't right, so show us the query you actually are using.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • What is the data type of the date column?  I would hazard a guess that it is stored as a varchar, and therefor not sorting as expected.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply