Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to set primary keys in descending date order?

Posted on 2004-09-10
3
Medium Priority
?
923 Views
Last Modified: 2012-06-22
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
Comment
Question by:iLvChopin
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
Jay Toops earned 500 total points
ID: 12030456
select dates from mytable
order by cast(dates as datetime)
0
 
LVL 10

Expert Comment

by:Jay Toops
ID: 12030470
sorry forgot descending

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

Jay
0
 

Author Comment

by:iLvChopin
ID: 12030828
this is an easy one. :-) Thank you!
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

971 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