June 20, 2018 at 9:09 am
I have few columns in my table. But i want to sort the data by two columns. My data looks like below-
A | 9/23/2014 |
A | NULL |
A | 9/23/2015 |
A | 9/29/2016 |
B | 10/1/2018 |
B | 9/29/2016 |
C | 10/29/2017 |
C | NULL |
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.
A | Null |
A | 9/23/2014 |
A | 9/23/2015 |
A | 9/29/2016 |
B | 9/29/2016 |
B | 10/1/2018 |
C | NULL |
C | 10/29/2017 |
June 20, 2018 at 9:14 am
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)
June 20, 2018 at 9:14 am
SELECT * FROM TABLE_NAME
ORDER BY 1,2
SELECT * FROM TABLE_NAME
ORDER BY FISRTCOLUMN, SECONDCOLUMN
***The first step is always the hardest *******
June 20, 2018 at 9:43 am
Both of these query not giving me correct result when there are NULLs.
June 20, 2018 at 9:46 am
Papil - Wednesday, June 20, 2018 9:43 AMBoth 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)
June 20, 2018 at 2:23 pm
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