I have a table and one of the fields has date format that stores values like: 2008-08-01
I need to select results that have current year, i.e. 2009.
How do I do that?
I'm sure that if your DBMS provides a date type then it also provides functions for working with that type. Consult its manual.
Not terribly helpful :-) How about this:
SELECT * FROM mytable WHERE ('DATE' >=2009-1-1) AND ('DATE' <2010-1-1);
It is a way, but not necessarily the best one.
The normal SQL function os extract() / date_trunc(). Note that if you don't tell me what db you're using, I'll assume it's postgresql. 🙂
In pgsql:
select * from table where date_trunc('year', tsfield) = '2009-01-01 00:00:00';
Or something like that.
Inkeye wrote:It is a way, but not necessarily the best one.
Not terribly helpful.
And if the DB is MySQL, and going off of Weedpacket's first post: a Google of 'mysql date and time functions' leads to the manual page including YEAR() including an example query.
for mySQL:
SELECT * FROM table WHERE YEAR(date) = YEAR(NOW())