Solved

how to sort on datetime datatype

Posted on 2008-06-18
3
1,169 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
ID: 21811369
Try this:

ORDER BY CAST(YouDateTimeField AS sql_variant)

Open in new window

0
 
LVL 33

Expert Comment

by:jppinto
ID: 21811370
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
ID: 21811387
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server Designer 19 40
Live Storage Vmotion VMs with shared VMDK 10 57
How to search for strings inside db views 4 27
SQL Query assistance 16 25
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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…

773 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