Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql match a year withing datetime field

Tags:

mysql

I am wondering if this is possible, in my database table, I have a datetime field, I wanting to query that table and pull in all the rows that have a posted date of 2011, however my date posted column is dateTime, so the form looks like this,

2011-06-12 00:00:00

How can I query the table to return all 2011 rows?

like image 367
Udders Avatar asked Oct 25 '11 12:10

Udders


People also ask

How can I compare two date fields in MySQL?

To count the difference between dates in MySQL, use the DATEDIFF(enddate, startdate) function. The difference between startdate and enddate is expressed in days.

How do I search a date range in MySQL?

If you need to select rows from a MySQL database' table in a date range, you need to use a command like this: SELECT * FROM table WHERE date_column >= '2014-01-01' AND date_column <= '2015-01-01';

How can get date in dd mm yyyy format in MySQL?

The MySQL DATE_FORMAT() function formats a date value with a given specified format. You may also use MySQL DATE_FORMAT() on datetime values and use some of the formats specified for the TIME_FORMAT() function to format the time value as well. Let us take a look at the syntax of DATE_FORMAT() and some examples.

How do I get the year from a date in MySQL?

Here’s the query you’d write: Use the YEAR () function to retrieve the year value from a date/datetime/timestamp column in MySQL. This function takes only one argument – a date or date and time. This can be the name of a date/datetime/timestamp column or an expression returning one of those data types.

What is datetime in MySQL?

Introduction to MySQL DATETIME data type You use MySQL DATETIME to store a value that contains both date and time. When you query data from a DATETIME column, MySQL displays the DATETIME value in the following format:

How to use the date function in MySQL to correct errors?

To correct it, you use the DATE function as follows: It returns one row as expected. In case the table has many rows, MySQL has to perform a full table scan to locate the rows that match the condition. To extract the time portion from a DATETIME value, you use the TIME function as the following statement:

How do I get only the year from the start_datetime column?

We’ll need to get only the year from the start_datetime column. We’ll use the YEAR () function. Here’s the query you’d write: Use the YEAR () function to retrieve the year value from a date/datetime/timestamp column in MySQL. This function takes only one argument – a date or date and time.


4 Answers

SELECT * FROM yourtable WHERE YEAR(datetime_column) = '2011' 

Read more about YEAR()

like image 117
Jason McCreary Avatar answered Oct 10 '22 14:10

Jason McCreary


It should work with datetime type too.

SELECT * FROM tblName WHERE YEAR(column) = '2011' 
like image 31
Ghazanfar Mir Avatar answered Oct 10 '22 15:10

Ghazanfar Mir


Try with:

SELECT *
  FROM your_table
 WHERE EXTRACT(YEAR FROM datetime_column) = '2011'
like image 28
hsz Avatar answered Oct 10 '22 16:10

hsz


You can even sub-string it if you want...

SELECT * FROM tblName WHERE substr(column,1,4) = '2011' 

I would however recommend creating a "date dimension" table, and then splitting your dateTime field into separate date and time fields, then joining to the "date dimension" table via the date field, which allows for much better functionality. See the answer I put on the following post for more information about using a "date dimension" table.

Select all months within given date span, including the ones with 0 values

You can join your table to it as follows;

SELECT tblName.*
FROM tblName
INNER JOIN dimDates AS dd
    ON tblName.dateField = dd.dd_date
    AND dd.dd_date = "2011"
like image 38
Dave Rix Avatar answered Oct 10 '22 14:10

Dave Rix