u936293
u936293

Reputation: 16264

Order by date desc, but within day order by time asc

I have a table in which there is a Date (smalldatetime) column. I need to order by Date DESC, but within each day to order by Date ASC. The output should be like:

Date
2017-01-31T09:00:00Z
2017-01-31T17:00:00Z
2017-01-28T09:00:00Z
2017-01-28T17:00:00Z
2017-01-25T09:00:00Z
2017-01-25T17:00:00Z
...

Is such a query possible?

If it's of any help, the data contains a maximum of two rows for the same Date.

Upvotes: 0

Views: 421

Answers (2)

D.Ming
D.Ming

Reputation: 1

ORDER BY DATE(date) DESC, TIME(date) ASC; I hope my answer can help you!

Upvotes: 0

Ofir Winegarten
Ofir Winegarten

Reputation: 9365

Cast to dateand then to time

ORDER BY 
    CAST([Date] AS date) DESC
    ,CAST([Date] AS time) ASC

Upvotes: 1

Related Questions