• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1182
  • Last Modified:

how to sort on datetime datatype

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
mugsey
Asked:
mugsey
  • 2
1 Solution
 
Mortaza DoulatyCommented:
Try this:

ORDER BY CAST(YouDateTimeField AS sql_variant)

Open in new window

0
 
jppintoCommented:
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
 
Mortaza DoulatyCommented:
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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now