Reputation: 2725
I have the table below:
+-------------+------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+-------------------+----------------+
| category_id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(64) | NO | | NULL | |
| description | text | NO | | NULL | |
| created_on | timestamp | NO | | CURRENT_TIMESTAMP | |
| updated_on | timestamp | YES | | NULL | |
+-------------+------------------+------+-----+-------------------+----------------+
I needed to delete some records according to the date created and executed a set of queries:
mysql> select created_on from categories limit 10;
+---------------------+
| created_on |
+---------------------+
| 2013-01-14 09:26:21 |
| 2012-08-15 11:18:38 |
| 2012-09-06 06:44:46 |
| 2012-09-06 23:27:14 |
then
mysql> select date('2013-01-14 09:26:21');
+-----------------------------+
| date('2013-01-14 09:26:21') |
+-----------------------------+
| 2013-01-14 |
+-----------------------------+
and finally I got:
mysql> select DATE('created_on') from categories limit 10;
+--------------------+
| DATE('created_on') |
+--------------------+
| NULL |
| NULL |
| NULL |
| NULL |
As you can see the date wasn't converted correctly. In about 10 minutes is worked ok:
mysql> select date(created_on) from categories limit 10;
+------------------+
| date(created_on) |
+------------------+
| 2013-01-14 |
| 2012-08-15 |
| 2012-09-06 |
| 2012-09-06 |
I'm sure that the data were intact while I was running the queries.
So my question:
Can anybody explain why date function showed different results on the same input?
Upvotes: 1
Views: 52
Reputation: 5843
select DATE('created_on') from categories limit 10;
String versus column:
select DATE(created_on) from categories limit 10;
Upvotes: 3
Reputation: 13455
You have to use::
select DATE(created_on) from categories limit 10;
By using the inverted comma, you are asking the database to treat 'created_on'
as parameter, and since it cannot be converted to date type, you are getting null
Upvotes: 3