Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract Month and Year in a MySQL date and compare them?

Tags:

date

sql

mysql

How do I extract the month and date from a mySQL date and compare it to another date?

I found this MONTH() but it only gets the month. I looking for month and year.

like image 339
JohnSmith Avatar asked Oct 20 '11 03:10

JohnSmith


People also ask

How do you compare dates in MySQL?

MySQL has the ability to compare two different dates written as a string expression. When you need to compare dates between a date column and an arbitrary date, you can use the DATE() function to extract the date part from your column and compare it with a string that represents your desired date.

What function can you use to compare dates in MySQL?

The DATE() function can be used for the comparison of the date of a number of purposes, some of them are: To extract the specific data on the basis of the date by using the inequality or equality signs. To extract the specific data on the basis of the date between different columns.

How do I display month and year in SQL?

There are two SQL function to do it: DATEPART() YEAR() and MONTH().

How can I get the difference between two dates in a month in MySQL?

PERIOD_DIFF() function MySQL PERIOD_DIFF() returns the difference between two periods. Periods should be in the same format i.e. YYYYMM or YYMM.


1 Answers

in Mysql Doku: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_extract

SELECT EXTRACT( YEAR_MONTH FROM `date` )  FROM `Table` WHERE Condition = 'Condition'; 
like image 91
betasux Avatar answered Oct 13 '22 00:10

betasux