wildzero
asked on
ADO database, order date field (which is text)
Hi 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 ?
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 ?
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 ?
I presume it's the same for your database ?
sorry:
read ... but no without the initial 0 before 1 and 5.
read ... but no without the initial 0 before 1 and 5.
ASKER
When I insert the date into the data base
ie
adoDB.FieldByName('date'). asString := DateToStr(dtp1.date);
what would be the way to do that then?
ie
adoDB.FieldByName('date').
what would be the way to do that then?
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 ....
ASKER
And if the date formatting is in the other way
ie
mm/dd/yyyy
would that still work?
ie
mm/dd/yyyy
would that still 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.
Could you not just convert the string to a date in the order by clause of your query?
ORDER BY CONVERT(DateTime, YourStringDate, 103)
ORDER BY CONVERT(DateTime, YourStringDate, 103)
agree with mikelittlewood
ASKER
yea thats how I thought you could do it
want to explain that some more
DateTime is like String or Boolean I presume
YourStringDate - would that be the column?
and whats 103?
want to explain that some more
DateTime is like String or Boolean I presume
YourStringDate - would that be the column?
and whats 103?
ASKER
When I just tried your code, I got an error - undefined fucntion CONVERT
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?
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?
ASKER
Using an ADO database calling the db via AdoQuery
The problem is the column is in VarChar (string) and I am trying to sort it but it's not sorting the Dates correctly...
So how will CONVERT(DateTime, '2/03/2006', 103) help?
Wouldn't it be better
Data type, Column Name, Formatting
The problem is the column is in VarChar (string) and I am trying to sort it but it's not sorting the Dates correctly...
So how will CONVERT(DateTime, '2/03/2006', 103) help?
Wouldn't it be better
Data type, Column Name, Formatting
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)
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)
Sorry maybe I was not clear the first time round so I apologise
ASKER
kk let me try that
ASKER
It looks good, and sounds good, but I get the error
EOleException - Undefined functoin 'CONVERT' in expression
:/
Points upped
EOleException - Undefined functoin 'CONVERT' in expression
:/
Points upped
ASKER
:)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Woah... I was quite skeptical about the cdate (as I did a quick search on Google and found nothing for it) however I tried it and it worked perfectly!
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).