I use VB.NET and SQL, how can I make a SQL query to obtain the records by year and month only? since normal queries are usually made by year, month and day, in this case I want to search for the records of '2016-10'
I use VB.NET and SQL, how can I make a SQL query to obtain the records by year and month only? since normal queries are usually made by year, month and day, in this case I want to search for the records of '2016-10'
You would have to compare the month and year with the column that holds the date, something like this:
Thus, with the MONTH function you get the month and with the YEAR function you get the year inside the date. So you would only have to compare with the values you want (10 and 2016 respectively).
In sentences
sql
as far as the conditionalwhere
goes with=
in that case your sentence would be like this:otherwise you could use date range with
BETWEEN
it like this:In itself it is easy, if you have a date type field you can work with it like this:
You have a date type field let's say a DATE() type field.
CURDATE(), which returns the date of the current day according to the system.
We combine them in the QUERY (query) and that's it.
Step to the Example: Suppose a table called employees, a field of type DATE() called miCumple, now, what they ask us to do is to calculate the birthdays of the month so we do the query:
That would be all, you can pass it to a Stored Procedure or launch it simply like this, since from now on it will be enough to call that function and at any time you want you will have the birthday boys of the month, I hope it helps you...