MDP MDP - 1 month ago 6
SQL Question

Sorting date based on condition on sql server

I have an Expiration date column on my table, I want to fetch records in order that are nearly to expire. I used 'ORDER BY DATE DESC' but it only arrange the date in descending order.

output using 'ORDER BY DATE'

2016-10-31
2016-10-06
2016-03-10


desired output:

2016-10-06
2016-10-31
2016-03-10

Answer

Is this what you want?

 order by abs(datediff(day, getdate(), [date]))
Comments