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
 sql select query order by date(field type varchar)

Author  Topic 

leet_uuk
Starting Member

2 Posts

Posted - 2010-04-22 : 05:33:59
Hi. I have sql server 2005 database table with 'date_recieved' coloumn varchar [50] .i'm storing dates in dd/mm/yyyy format.

I'm writing a sql query to retrive rows in order by date but failing. Its something like

SELECT *
FROM question_entry
ORDER BY CONVERT(datetime, 'date_received', 1);


Can any one guide how to convert/order by data type from varchar to datetime on the fly in sql query??

malpashaa
Constraint Violating Yak Guru

264 Posts

Posted - 2010-04-22 : 05:41:09
Try this:

SELECT *
FROM question_entry
ORDER BY CONVERT(DATETIME, 'date_received', 103);

Although I do not recommend you to store datetime values in varchar fields. So consider redsign your table to store datetime values in datetime fields.
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-04-22 : 05:42:36
Duplicate post:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=143375
Go to Top of Page
   

- Advertisement -