views:

51

answers:

3

Dear Stackoverflow users and contributers,

I have a problem extracting the year out of a mysql timestamp field. I managed to get it work with a datetime field using this method:

SELECT id FROM TABLE WHERE YEAR(creation_date) = 2010

CREATE TABLE IF NOT EXISTS `pub_media` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `parent_id` int(11) DEFAULT NULL,
  `title` text,
  `filename` text,
  `path` text,
  `serv_path` text,
  `type` enum('images','videos','files','audio','gallery') DEFAULT NULL,
  `keywords` text,
  `label_id` int(11) DEFAULT NULL,
  `creation_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `update_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
  `rank` int(11) NOT NULL DEFAULT '0',
  `deleted` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Server version : 5.1.41 Is there a simalar way to perform this action on a timestamp field? I want to keep this action in my SQL statement and not try to move it to PHP or any other scripting language, if possible.

Thx in advance.

+3  A: 

use CAST function :)

SELECT id FROM TABLE WHERE YEAR(CAST(creation_date AS DATE))=2010

should work with creation_date timestamp

[[edit]] ,added () around cast

dweeves
I'm getting this error: 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 'creation_date AS DATE) = 2010 AND deleted = 0 ORDER BY rank DESC' at line 1using this query:SELECT id,title FROM pub_media WHERE YEAR(CAST creation_date AS DATE) = 2010 AND deleted = 0 ORDER BY rank DESCmysql v. 5.1.41
Sam Vloeberghs
hey you guys , this is not working , stop pulling it up :)
Sam Vloeberghs
added forgotten () on CAST
dweeves
yes that's it. Thx dweeves!
Sam Vloeberghs
This solution works , but using just the YEAR function works to , see approved answer post
Sam Vloeberghs
+1  A: 

What problem are you encountering, and can you include the output of CREATE TABLE in your question? This works for me on MySQL 5.1.41-3ubuntu12.3:

DROP TABLE IF EXISTS `table1`;

CREATE TABLE `table1` (
    `id` int(11) NOT NULL auto_increment,
    `ts` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY  (`id`)
) ENGINE=InnoDB;

INSERT INTO table1 (id) VALUES (1);

SELECT * FROM table1;
+----+---------------------+
| id | ts                  |
+----+---------------------+
|  1 | 2010-07-05 15:32:11 |
+----+---------------------+

SELECT id FROM table1 WHERE YEAR(ts) = 2010;
+----+
| id |
+----+
|  1 |
+----+
Mike
heck no, I don't know why it ain't working on my DB. Maybe because I use MyISAM instead of InnoDB as the DB Engine?
Sam Vloeberghs
I've just tried it with MyISAM, and that works too. Can you edit your question, and include the output from `CREATE TABLE <table>`? Also, what version of MySQL are you using?
Mike
added both requests
Sam Vloeberghs
It works as I would expect with your table structure. I've just copied and pasted it into my copy of MySQL, inserted a record, and selected from it with no need for casting or conversion.
Mike
I do believe that but that does not make it work in my case though..
Sam Vloeberghs
As we are both using MySQL 5.1.41, that is very odd. Do you get an error message, or just an empty result set?
Mike
dude , it fricking works.. I tried it again using my phpmyadmin and there it worked fine.. I really haven't got a clue about what I was doing wrong..
Sam Vloeberghs
It doesn't work on my system, that was the first thing I tried, and it returned zero results!doing "SELECT YEAR(ts) FROM table1" does return the correct information, but putting the "YEAR(ts)" in the WHERE clause returns nothing... Maybe it's a collation thing?
Dave Rix
odd, just messed around with my query, tried again, and hey-presto, it now works with "WHERE YEAR(ts) = '2010'"... most odd!
Dave Rix
"Stranger and stranger," said Alice. I tried it with and without quotes around the year, and both worked.
Mike
+1  A: 

The following works ok,

SELECT id FROM TABLE WHERE DATE_FORMAT( creation_date,  "%Y" ) = "2010"

Formatting the data is really useful for queries like this, I have used it numerous times for breaking data down to 10 minute intervals for example...

Dave Rix
than someone should do a speedtest on what is the fastest, using DATE_FORMAT or TYPE cast.. :p
Sam Vloeberghs