Query to search by month and year

Usage VB.NET and SQL, How can I do a SQL query to get the records by year and month only? since usually the normal queries are done by year, month and day, in this case I want to look up the records of '2016-10'

 7
Author: Ivxn, 2016-04-20

3 answers

You would have to compare the month and year with the column that saves the date, something like this:

SELECT * FROM tabla WHERE MONTH(colfecha) = 10 AND YEAR(colfecha) = 2016

Thus, with the MONTH function you get the month and with the Year function you get the year within the date. So you only have to compare with the values you want (10 and 2016 respectively).

 9
Author: Alvaro Montoro, 2016-04-20 18:32:43

In sentences sql as far as the conditional where goes with = in that case your sentence would look like this:

SELECT * FROM COMISION where month='10' and year='2016'

Otherwise you could use date range with the BETWEEN like this:

SELECT * FROM COMISION where (tu_campo_fecha) BETWEEN '2016-10-01' AND '2016-10-31'
 7
Author: Fernando M. Goycochea, 2016-04-26 13:53:44

In if it is easy, if you have a field of type date you can work with the like:

  1. You have a field of type date suppose a field of type DATE ().

  2. CURDATE(), which returns the date of the current day according to system.

  3. We combine them in the QUERY and that's it.

Step To example: Suppose a table called employees, a field of type DATE() called miCumple, now, what we are asked to do is calculate the birthday of the month then we make the query:

select * from empleados where MONTH(miCumple) = MONTH(CURDATE());

That would be all, you can pass it to a Stored Procedure or launch it just like that, since from now on it will be enough to call said function and at any time you want you will have the birthday of the month, I hope it will serve you...

 3
Author: Rafael Castro, 2019-01-17 21:09:28