Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 944
  • Last Modified:

How to set primary keys in descending date order?

Here is a table I am working with:

dates         char           20
filename    text            16

It basically logs date and filename a user uploads. However the "dates" field was originally set as Char type. If I do set the primary key as descending order, it sorts like this:

9/3/2004
9/2/2004
9/1/2004
9/11/2004
9/12/2004
...etc

which is not the correct date order. The correct date order should be:

9/12/2004
9/11/2004
9/3/2004
9/2/2004
9/1/2004

It didn't seem I can change the Char type to Datetime type. In this case, how can I sort the primary keys in descending date order??  Thank you very much!!

0
iLvChopin
Asked:
iLvChopin
  • 2
1 Solution
 
Jay ToopsCommented:
select dates from mytable
order by cast(dates as datetime)
0
 
Jay ToopsCommented:
sorry forgot descending

select dates from mytable
order by cast(dates as datetime)  desc

Jay
0
 
iLvChopinAuthor Commented:
this is an easy one. :-) Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

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