1

This is the SELECT I'm using (that's not working)

SELECT 
    IF( 
         TIME( `date_time` ) = '00:00:00', 
         DATE( `date_time` ), 
         `date_time`
    ) AS date_time
FROM `table`

My Table:

CREATE TABLE `graphs` (
  `graph_id` int(11) NOT NULL auto_increment,
  `company_id` int(11) NOT NULL,
  `stat_book_id` int(11) NOT NULL,
  `object_id` int(11) NOT NULL,
  `object_type` varchar(50) NOT NULL,
  `created_user_id` int(11) NOT NULL,
  `name` varchar(50) NOT NULL,
  `status` tinyint(1) NOT NULL default '1',
  `sparkline` varchar(255) NOT NULL,
  `sparkline_updated` datetime NOT NULL,
  `date_created` datetime NOT NULL,
  `date_updated` timestamp NOT NULL default '0000-00-00 00:00:00' on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`graph_id`),
  KEY `stat_book_id` (`stat_book_id`,`object_id`,`object_type`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

Version data

Server version: 5.0.91-community
time format: %H:%i:%s

This query:

SELECT 
    `date_time`, DATE(`date_time`), TIME(`date_time`) 
FROM `graph_values` 
LIMIT 10

Returns:

date_time   DATE(`date_time`)   TIME(`date_time`)
2010-09-24 00:00:00     2010-09-24  00:00:00
2010-09-19 00:00:00     2010-09-19  00:00:00
2010-09-16 21:05:00     2010-09-16  21:05:00
2010-10-30 00:00:00     2010-10-30  00:00:00
2010-10-08 00:00:00     2010-10-08  00:00:00
2010-09-17 23:50:00     2010-09-17  23:50:00
2010-09-15 00:00:00     2010-09-15  00:00:00
2010-10-23 00:00:00     2010-10-23  00:00:00
2010-09-17 19:24:00     2010-09-17  19:24:00
2010-09-19 19:24:00     2010-09-19  19:24:00

This query, however, returns incorrect results:

SELECT 
    `date_time`, DATE(`date_time`), TIME(`date_time`), IF( 
        TIME( `date_time` ) = '00:00:00', 
        DATE( `date_time` ), 
        `date_time`
    )
FROM `graph_values`
LIMIT 10

Results:

date_time   DATE(`date_time`)   TIME(`date_time`)   IF( TIME( `date_time` ) = '00:00:00', DATE( `date_time` ), `date_time` )
2010-09-24 00:00:00     2010-09-24  00:00:00    2010-09-24 00:00:00
2010-09-19 00:00:00     2010-09-19  00:00:00    2010-09-19 00:00:00
2010-09-16 21:05:00     2010-09-16  21:05:00    2010-09-16 21:05:00
2010-10-30 00:00:00     2010-10-30  00:00:00    2010-10-30 00:00:00
2010-10-08 00:00:00     2010-10-08  00:00:00    2010-10-08 00:00:00
2010-09-17 23:50:00     2010-09-17  23:50:00    2010-09-17 23:50:00
2010-09-15 00:00:00     2010-09-15  00:00:00    2010-09-15 00:00:00
2010-10-23 00:00:00     2010-10-23  00:00:00    2010-10-23 00:00:00
2010-09-17 19:24:00     2010-09-17  19:24:00    2010-09-17 19:24:00
2010-09-19 19:24:00     2010-09-19  19:24:00    2010-09-19 19:24:00

Anyone have an idea why?


UPDATE: Solution found.

It seems there is a bug or it is supposed to be the above way. It seems the problem is it can't return a date format in two different ways (one with time, one without). It seems that the older versions of MySQL could (see the below tests).

The same test was done in a SQL database and it had the same problem. The simple solution is the following (I believe it's comparable execution-time-wise):

SELECT 
     IF( 
          TIME( `date_time` ) = '00:00:00', 
          REPLACE( `date_time`, ' 00:00:00', '' ), 
          `date_time`
     ) AS date_time
FROM `table`
8
  • If the time is 0, you only want the date. But else you only want the time? Are you sure that else you don't want date and time?
    – Konerak
    Nov 17, 2010 at 7:42
  • @Kerry - perfectly alright, unless SELECT IF(..) as date_time, date_time, the later date_time column override the first column
    – ajreal
    Nov 17, 2010 at 7:45
  • @ Konerak -- you are right, but it still isn't working -- I updated the question. Nov 17, 2010 at 7:48
  • @ajreal -- that's acting as an alias, not as a name, I can change it and it still has the same funky results. Nov 17, 2010 at 7:48
  • @Kerry - Results returned via api will override it. Anywhere, what is the data type?
    – ajreal
    Nov 17, 2010 at 7:53

1 Answer 1

3

I'm thinking you're meaning to do this:

SELECT 
     IF( 
          TIME( `date_time` ) = '00:00:00', 
          DATE( `date_time` ), 
          `date_time` 
     ) AS date_time
 FROM `table`

So that

2010-11-17 08:43:00 -> 2010-11-17 08:43:00
2010-11-17 00:00:00 -> 2010-11-17

Full-blown example:

CREATE TABLE `testdates` ( `date_time` datetime NOT NULL);

insert into testdates values (now());
insert into testdates values ("2010-11-17 00:00:05");
insert into testdates values ("2010-11-16 00:00:00");
insert into testdates values ("2010-11-15");

SELECT 
     IF( 
          TIME( `date_time` ) = '00:00:00', 
          DATE( `date_time` ), 
          `date_time` 
     ) AS date_time
 FROM testdates;

+---------------------+
| date_time           |
+---------------------+
| 2010-11-17 08:52:06 |
| 2010-11-17 00:00:05 |
| 2010-11-16          |
| 2010-11-15          |
+---------------------+

My configuration:

mysql> show variables like "version";
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| version       | 5.0.26-log |
+---------------+------------+
1 row in set (0.00 sec)

mysql> show variables like "time_format";
+---------------+----------+
| Variable_name | Value    |
+---------------+----------+
| time_format   | %H:%i:%s |
+---------------+----------+
1 row in set (0.00 sec)
4
  • You're right, but it's still giving the full date, despite that every record in the table, right now, has 00:00:00 as the hour. Nov 17, 2010 at 7:47
  • It's working here. Can you show your SHOW CREATE TABLE statement? What type is the column?
    – Konerak
    Nov 17, 2010 at 7:49
  • @ Konerak -- that's really interesting, I used your code exactly (copy and pasted) and it didn't work. What version of MySQL are you using? Nov 17, 2010 at 7:56
  • @Kerry: Tested on 5.0.26-log and 5.0.51a-24+lenny2-log. Can you just do a SELECT date_time, DATE(date_time), TIME(date_time) from table for me?
    – Konerak
    Nov 17, 2010 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.