Tested in a listbox pasting the data you supplied: with sorted set to true: with a leading zero the dates are correctly sorted but no with the initial 0 before 1 and 5.
I presume it's the same for your database ?
Main Topics
Browse All TopicsHi there,
I have a date field in my database but it's set to text (due to a number of reseaons) and it works perfect for everything I do - except, when I try and order the date field it doesn't work as excepted
like I get
2/03/2006
21/03/2006
24/03/2006
5/03/2006
Is there a way I can sort the date field as date
ie
ORDER BY (date) AS Date ?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
var
Str : String;
begin
str:= '1/03/2006';
Str := StringOfChar('0', 10 - Length(Str)) + Str;
// Str returns 01/03/2006 - the date + separators is 9 chars, 0 is added
str:= '12/03/2006';
// Str returns 12/03/2006 - the date + separators is already 10 chars, no 0
end;
In your code, it becomes sth like:
adoDB.FieldByName('date').
I presume it should work ....
You're right. Better, with FormatDateTime
d = 1 / dd = 01
m = 3 // mm = 03
var
S, T: String ;
begin
S := FormatDateTime('dd/mm/yyyy
T := FormatDateTime('mm/dd/yyyy
S = 01/02/2006
T = 02/01/2006
in your code, should be sth like:
adoDB.FieldByName('date').
or
adoDB.FieldByName('date').
Test and see if it works.
Ok I assumed you are using an SQLServer database as this is a standard function.
the function works like this:
Parameter 1, data type to convert to
Parameter 2, data to convert from string
Parameter 3, date time format (in your case dd/mm/yyyy)
CONVERT(DateTime, '2/03/2006', 103)
What database are you using?
Because CONVERT(DateTime, '2/03/2006', 103) will change everyone of the string dates to proper datetime and THEN order it afterwards.
You can also then choose to do it up or down
ORDER BY CONVERT(DateTime, FIELDNAME, 103)
or
ORDER BY CONVERT(DateTime, FIELDNAME, 103) DESC
Notice you dont actually put in an explicit string into the fieldname part, put the name of the column holding the string dates
ie
MYDATECOLUMN
'2/03/2006'
'1/03/2006'
'10/03/2006'
'3/03/2006'
ORDER BY CONVERT(DateTime, MYDATECOLUMN, 103)
Business Accounts
Answer for Membership
by: bernaniPosted on 2006-03-20 at 13:59:52ID: 16241326
Hi
Have you try to add a 0 before the days in the range 1 to 9 ? In this way, your numbers are sorted in the good order (adding a leading zero).