Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to sort a date field using web sql query?
query = 'SELECT * FROM invoice ORDER BY inv_date';Original table:
C#
id   inv_date    code    amt1
1   03/03/2003  C00002   400   
2   02/02/2001  C00002   300   
3   12/02/2000  C00001   300  


Sorted table:
C#
id  inv_date    code   amt1
3  02/02/2001  C00001  300  
1  03/03/2003  C00002  400  
2  12/02/2000  C00002  300  

This is the output after sorting how can i sort properly from smallest date to higher?
Can anybody please help me to trigger this out?
Posted
Updated 15-Jul-14 21:29pm
v2

Simple: change your database.

Stop storing date (and other numeric values) as strings: store them as an appropriate numeric datatype - in this case DATETIME - and you will be able to sort and compare them without problems.

At the moment, they are strings - NVARCHAR - so the comparison that is used at all times, including for sorting, is a string comparison. Which works by looking at the two values character by character and bases the whole comparison on the first different pair of characters.

It would be possible to convert the date string to a DATETIME for sorting, but that relies on the values always being correct - which is not necessarily correct with strings as it is far too easy to get "bad" data in there.

Sort your DB out - and always use appropriate datatypes!
 
Share this answer
 
sir, best way to store the value lets say "16/07/2014" is like this in numeric format "20140716" and at time of sorting it will manage everything on its own.
 
Share this answer
 

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