views:

66

answers:

3

The title says it all; is there an easy (single query) way to do this?

I'm reading those values from a column in a table and I think that the column itself is defined as a string (can't be helped, i'm afraid).

Thanks guys.

+2  A: 

Use UNIX_TIMESTAMP;

SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');

Update:

SELECT UNIX_TIMESTAMP(CAST(fieldName AS DATE));
Sarfraz
Hmmm great but I should have mentioned i'm reading those values from a column in a table and I think that the column itself is defined as a string. My bad, sorry
Hal
Will it recognize the MM/DD/YYYY format automatically?
Hal
@Hal: Yes because we have converted it to DATE
Sarfraz
I suppose (haven't checked it) it assumes the format specified in the `%Y-%m-%d` variable (`SELECT @@date_format`). In my server it is `%Y-%m-%d`.
Álvaro G. Vicario
+1  A: 
SELECT '12/31/10',
    STR_TO_DATE('12/31/10', '%m/%d/%y'),
    UNIX_TIMESTAMP(STR_TO_DATE('12/31/10', '%m/%d/%y'))

Both functions are covered here: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

Álvaro G. Vicario
A: 
SELECT UNIX_TIMESTAMP(STR_TO_DATE('08/05/10','%m/%d/%y'));
Wrikken