Solved

How to set primary keys in descending date order?

Posted on 2004-09-10
3
876 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 125 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL SELECT query help 7 41
Tsql query 6 22
transaction in asp.net, sql server 6 33
MS SQL Server COnditional Where statement 7 59
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

777 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