Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing dates by month and year in mysql

Tags:

sql

mysql

I have a table containing data about events and festivals with following columns recording their start and end dates.

  • Start_Date
  • End_Date

date format is in YYYY-MM-DD. I need to fetch event details with the following condition.

  • Need to fetch all events which start with a current month and there end dates can be anything say currentDate+next30days.

I am clear about end date concept. but not sure how I can fetch data whose start dates are in a current month. For this, I need to compare current year and current month against the Start_Date column in my database.

Can anyone help me to point out as how I can do that?

like image 462
Umesh Awasthi Avatar asked Oct 06 '13 06:10

Umesh Awasthi


People also ask

How do you compare dates with month and year?

As below screenshot shown, supposing you need to compare two date lists and match the dates by month and year only, you can apply the below formula to achieve it. 1. Select a blank cell, enter formula =MONTH(A2)&YEAR(A2)=MONTH(B2)&YEAR(B2) into the Formula Bar, and then press the Enter key.

How can I compare two dates 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. In this case, the enddate is arrival and the startdate is departure .

How do you compare two dates in a query?

In SQL, the date value has DATE datatype which accepts date in 'yyyy-mm-dd' format. To compare two dates, we will declare two dates and compare them using the IF-ELSE statement. We can declare variables easily by using the keyword DECLARE before the variable name.


1 Answers

select * from your_table
where year(Start_Date) = year(curdate())
and month(Start_Date) = month(curdate())
and end_date <= curdate() + interval 30 day
like image 57
juergen d Avatar answered Oct 05 '22 12:10

juergen d