Solved

how to sort on datetime datatype

Posted on 2008-06-18
3
1,165 Views
Last Modified: 2008-06-18
How can I sort on a datetime datatype

11/01/2002 09:35:19
11/01/2002 11:35:10
11/01/2002 07:35:09

order by does not give the correct results.   I need to be able to sort on date then the time part of the date
0
Comment
Question by:mugsey
  • 2
3 Comments
 
LVL 12

Expert Comment

by:Mortaza Doulaty
Comment Utility
Try this:

ORDER BY CAST(YouDateTimeField AS sql_variant)

Open in new window

0
 
LVL 33

Expert Comment

by:jppinto
Comment Utility
If you have the data as Date/Time format it will sort correctly. If you have your data as text it will not sort correctly.

jppinto
0
 
LVL 12

Accepted Solution

by:
Mortaza Doulaty earned 500 total points
Comment Utility
But simply using ORDER BY should work...

It displays:

2002-11-01 07:35:09.000
2002-11-01 09:35:19.000
2002-11-01 11:35:10.000

Isn't this what you expect?
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now