views:

199

answers:

2

In MySQL 4.0.21-standard, I have a table with a date, saved as a string.

I want to compare this string with a date in my request.

SELECT FE_CLIENT.*
FROM FE_CLIENT
WHERE D_DATFINPUBLI < '2010/06/03'

How can I cast my column date_deb to a date for compare?

+1  A: 

Assuming MySQL (if not, retag your question)

Use the MySQL STR_TO_DATE function to put the '2010/06/03' to a DATETIME value.

SELECT FE_CLIENT.*
FROM FE_CLIENT
WHERE D_DATFINPUBLI < STR_TO_DATE('2010/06/03','%Y/%m,%d');

Do the same thing for D_DATFINPUBLI if it's not already a DATETIME format.

EDIT:

SELECT STR_TO_DATE( D_DATFINPUBLI, '%d/%m/%Y %h:%i' ) DD, FE_CLIENT . * 
FROM FE_CLIENT
WHERE STR_TO_DATE( D_DATFINPUBLI, '%d/%m/%Y %h:%i' ) < STR_TO_DATE( '04/06/2010', '%d/%m/%Y' ) 
AND D_CDSTATUPUBLI <> 'EXP'
ORDER BY D_NIDPUBLI
Konerak
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(D_DATFINPUBLI,'%d/%m/%Y') < STR_TO_DATE('03/06/2010','%d/%m/%Y
Mercer
What format is D_DATFINPUBLI in now? Can you post the entire query?
Konerak
SELECT FE_CLIENT.* FROM FE_CLIENTWHERE STR_TO_DATE(D_DATFINPUBLI,'%d/%m/%Y') < STR_TO_DATE('03/06/2010','%d/%m/%Y')AND D_CDSTATUPUBLI <> 'EXP' ORDER BY D_NIDPUBLID_DATFINPUBLI in my table like this '03/06/2010 11:12'
Mercer
I updated the query. The STR_TO_DATE function was added in MySQL version 4.1.1 - make sure your database version is higher. Test: `SELECT STR_TO_DATE('03/06/2010','%d/%m/%Y')`
Konerak
just this line not work for meSELECT STR_TO_DATE( D_DATFINPUBLI, '%d/%m/%Y %h:%i' ),FE_CLIENT.*FROM FE_CLIENT;
Mercer
MySQL 4.0.21-standard :(
Mercer
I'm sorry, either you follow the good advice of Shrapnel below, or you'll have to play with strings (create the date with calculations using substring OR compare the strings)
Konerak
A: 

Just format your string to proper format before query execution

or, if you want it strictly with mysql,

WHERE D_DATFINPUBLI < replace('2010/06/03','/','-')

EDIT:

D_DATFINPUBLI field must be of date type and have format of 2010-06-03

Col. Shrapnel
D_DATFINPUBLI in my table like this '03/06/2010 11:12' and i want WHERE D_DATFINPUBLI < '03/06/2010'
Mercer
ahahahaha! so, you have no date in your database. change the value in this field to proper date format
Col. Shrapnel
i can't change this data in my DataBase
Mercer
@Mercer no, you can. You just lazy because you don't understand the importance of this change. Don't be silly. that's the only solution
Col. Shrapnel
it's not me who decides that an old application if I change the fields I should board in all code source
Mercer
Yes, you should. Anyway you would, sooner or later.
Col. Shrapnel
@Mercer thanks for accepting. I should've been explain it a bit. When you have it in the date format, Mysql will find the row at once. And with your current format it have to sort out the whole table, one row by one. It will kill your database pretty soon
Col. Shrapnel