3

I try to fetch date from table in descending order but when i write order by clause in query this shows date in ascending order .. i can not post whole query i post specific line e.g. order by .. when i tried this is what query shows

 date
    01/03/2016
    26/02/2016
    25/02/2016
    06/01/2015

but this is what i want in re

 date
    06/01/2015
   25/02/2016
    26/02/2016
    01/03/2016

this is what i tried

 ORDER BY
convert(date, account.postdate) DESC,      
account.ACDOCNO ASC     

any solution?

Bakhtawar
  • 75
  • 1
  • 10

3 Answers3

0

You need to change your date in ORDER BY from DESC to ASC

ORDER BY
convert(date, account.postdate) ASC,   
account.ACDOCNO ASC   

or simply

ORDER BY
convert(date, account.postdate) ,   
account.ACDOCNO 

As ASC is the default sorting order in SQL Server.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

I think the problem here may be a language problem - the desired output you've shared is indeed in ascending order (which for dates means older to newer). So just use asc instead of desc and you should be OK:

ORDER BY
convert(date, account.postdate) ASC, -- Here!
account.ACDOCNO ASC     
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • but asc means higher to lower – Bakhtawar Mar 08 '16 at 07:35
  • @Bakhtawar no, that's where your mistake it. `asc` (short for ascending) means lower to higher – Mureinik Mar 08 '16 at 07:36
  • @Bakhtawar to quote [SQL Server's documentation](https://msdn.microsoft.com/en-us/library/ms188385.aspx) - "ASC sorts from the lowest value to highest value. DESC sorts from highest value to lowest value." – Mureinik Mar 08 '16 at 07:37
  • i have a amount column when i put this line convert(date, account.postdate) ASC, in query then amount value change when i erase these values in query then correct amount is displayed but when i put these lines amount is changed ... why is this occur? – Bakhtawar Mar 08 '16 at 08:45
  • when i write this ORDER BY account.ACDOCNO ASC, convert(date, account.postdate) ASC date not order by as asc.. when i write convert line above account.acdocno dates and amount changes.. – Bakhtawar Mar 08 '16 at 09:43
0

It also depends how you save the dates in the table. Do you save them as type DATETIME?

If not here is a related post: Order by date (varchar)?

By the way, what is the convert() doing here? From what I've checked in the reference its used for converting string into different character sets.

Community
  • 1
  • 1
Björn Grambow
  • 368
  • 1
  • 14