Mercer
Mercer

Reputation: 9986

compare string to date on mysql

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?

Upvotes: 1

Views: 13639

Answers (2)

Your Common Sense
Your Common Sense

Reputation: 157875

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

Upvotes: 1

Konerak
Konerak

Reputation: 39773

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

Upvotes: 5

Related Questions