selez
selez

Reputation: 97

How to select year in sql statement

My code:

 $results = $GLOBALS['wpdb']->get_results( 'SELECT * FROM myTable WHERE date = 2014 ORDER BY id DESC', object );

The problem is date is stored in this format: 2014-01-01 So how do I select just the year ( I don't care about month and day for the time being ).

Thanks

Upvotes: 0

Views: 2804

Answers (5)

Fatih SARI
Fatih SARI

Reputation: 433

You can select year for get posts with query_posts(); parameter is year. Example: query_posts("year=2014"); This is not full question for you, only alternative..

Upvotes: -1

user260523
user260523

Reputation: 145

Try this Query :

SELECT * FROM myTable WHERE year(`date`)='2014' ORDER BY id DESC 

Upvotes: 1

Ed Massey
Ed Massey

Reputation: 449

To select all rows where the year of a date column (called date_col) is equal to 2014 use the year function:

SELECT * FROM `tbl` WHERE Year(`date_col`) = '2014';

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269953

Use the year() function:

WHERE year(date) = 2014

or use explicit comparisons:

WHERE (date >= '2014-01-01' and date < '2015-01-01')

The latter is better because it can make use of an index on the date column.

Upvotes: 2

SMA
SMA

Reputation: 37023

Try this:

SELECT * FROM myTable WHERE date >= '2014-01-01 00:00:00' ORDER BY id DESC

Upvotes: 0

Related Questions