Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 ORDER By date

Author  Topic 

SuperSQLNewb
Starting Member

9 Posts

Posted - 2009-03-10 : 15:34:17
Hi am trying to order my query by date. The problem though is that my date is formatted like: dd/mm/yy

So it gets order like:
02/06/08
02/11/09
02/13/08
02/18/09

And I want:
02/06/08
02/13/08
02/11/09
02/18/09

I have tried this: SELECT CONVERT(varchar,ReviewDate,111) AS Date FROM Table ORDER BY CONVERT(varchar,ReviewDate,111)

But that does not see to do anything. ReviewDate is VarChar :(

Thanks for any help!

NeilG
Aged Yak Warrior

530 Posts

Posted - 2009-03-10 : 15:40:39
Have you also added on ASC to order the dates in ascending order
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2009-03-10 : 15:41:19
Try converting it to a datetime instead of varchar in your order by.
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-03-10 : 15:45:45
Try this...

SELECT ReviewDate FROM Table ORDER BY cast(ReviewDate as datetime) ASC
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-03-10 : 15:46:27
Ohh...dint realize Rick had mentioned it earlier....had the reply window open for sometime i guess.
Go to Top of Page

SuperSQLNewb
Starting Member

9 Posts

Posted - 2009-03-10 : 16:17:57
SELECT ReviewDate FROM Table ORDER BY cast(ReviewDate as datetime) ASC

This worked! Thank you very much!
Go to Top of Page
   

- Advertisement -