Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Select Top 10 * from News_Events order by NewsDate;

I have tried the above query; but the output is
1/10/2011 12:00:00 AM
1/11/2011 12:00:00 AM
2/10/2011 12:00:00 AM
2/11/2011 12:00:00 AM
3/10/2011 12:00:00 AM
3/11/2011 12:00:00 AM


NewsDate data-type is smalldatetime.

Anyone please suggest me some way to solve this.

Thanks in advance.
Posted
Updated 9-Nov-11 20:15pm
v2
Comments
RaisKazi 10-Nov-11 2:16am    
Probably it's in "mm/dd/yyyy" format, and if it is in "mm/dd/yyyy" format then result is correct.
sahabiswarup 10-Nov-11 4:26am    
date format is dd/mm/yyyy
Amir Mahfoozi 10-Nov-11 10:26am    
Could you please show the output of "Select *, CONVERT(varchar, newsdate), from News_Events order by NewsDate;"

Check your date column is datetime instead of a varchar and your sorting will be done correctly and faster than sorting strings.

Also the date view format could be mm/dd/yyyy and not dd/mm/yyyy like RaisKazi said.
 
Share this answer
 
Comments
P.Salini 10-Nov-11 4:18am    
She has mentioned that datatype is smalldatetime
Mehdi Gholam 10-Nov-11 4:21am    
No harm in rechecking :)
sahabiswarup 10-Nov-11 4:25am    
P.Salini there is some mistake by u to identify me, i'm a guy.
sahabiswarup 10-Nov-11 4:21am    
yes...i have already mentioned that NewsDate datatype is smalldatetime
If you are sure that the type of the field is smalldatetime so the output is surely correct so to convince yourself that its correct try running this SQL which will show you the month names and you will understand that the problem is, probably, in the way you are looking to those numbers :

SQL
Select  * , CONVERT(varchar, newsdate),
             from News_Events order by NewsDate;
 
Share this answer
 
Try this
SQL
Select Top 10 * from News_Events order by convert(datetime,NewsDate);
 
Share this answer
 
Comments
sahabiswarup 10-Nov-11 5:19am    
still i get the same output:

2011-01-10 00:00:00
2011-01-11 00:00:00
2011-02-10 00:00:00
2011-02-11 00:00:00
2011-03-10 00:00:00
2011-03-11 00:00:00
Heino Zunzer 10-Nov-11 6:33am    
yes, and what's your concern with that? That is the correct order.
the Format here is YYY-MM-DD

let me translate:
2011-01-10 = January 10, 2011
2011-02-10 = February 10, 2011
2011-03-10 = March 10, 2011

The format in your original post was MM/DD/YYYY (US format)

So you see, the order is correct.
sahabiswarup 10-Nov-11 7:19am    
these above solution doesn't solve my problem.
Heino Zunzer 11-Nov-11 4:47am    
It's not a solution, it's an explanation.
Because I don't know, what your problem is.
THE DATES ARE IN THE CORRECT ORDER!

1/10/2011 12:00:00 AM -> Jan. 10
1/11/2011 12:00:00 AM -> Jan. 11
2/10/2011 12:00:00 AM -> Feb. 10
2/11/2011 12:00:00 AM -> Feb. 11
3/10/2011 12:00:00 AM -> Mar. 10
3/11/2011 12:00:00 AM -> Mar. 11

How else would you want them sorted?
Do you want them displayed in another format? DD/MM/YYYY?

Please state what output you do expect. Then I can (maybe) tell you, how to get there. but sorting is not your problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900