Mohammad Shahbaz
Mohammad Shahbaz

Reputation: 423

Date sorting in SQL Server

I have column which stores date in format of mm/dd/yyyy.

When I retrieve value from this table order by date then it returns the dates in descending order of month but ascending order of day.

e.g.

date
=========
09/20/2018
09/21/2018
09/22/2018
10/15/2018
10/16/2018
08/07/2018
08/06/2018

select * from table order by date;

Current result:

10/15/2018
10/16/2018
09/20/2018
09/21/2018
09/22/2018
08/06/2018
08/07/2018

Expected result:

10/16/2018
10/15/2018
09/22/2018
09/21/2018
09/20/2018
08/07/2018
08/06/2018

Upvotes: 1

Views: 205

Answers (3)

Yashar Aliabbasi
Yashar Aliabbasi

Reputation: 2739

Simple use DESC after ORDER BY like this select * from table order by date DESC

Upvotes: 0

sumit
sumit

Reputation: 15464

You can need to convert your column to date before sorting

select * from table order by CONVERT(datetime, date) DESC

Upvotes: 2

Fahmi
Fahmi

Reputation: 37493

use order by desc and use cast so whatever your data type is - it will convert it into date and then order by desc

select * from table order by cast([date] as date) desc

Upvotes: 1

Related Questions