I have a table with one of the columns as a date in the format "YYYY-MM-DD". Can I use select to get all the data in a monthly range? Let's say I want all the data from 2012-01-xx to 2013-04-xx. So I'm basically looking for an SQL query like the one below:
SELECT * FROM table WHERE date IN BETWEEN '2012-01' AND '2013-04' (INVALID QUERY)
Since each month starts with “01,” I can modify the above query to adjust the start condition.
SELECT * FROM table WHERE date IN BETWEEN '2012-01-01' AND '2013-04' (INVALID QUERY)
Now the problem is with enddate. I need to manually calculate the last date for this month, taking into account all factors, such as the length of the month, leap year, etc., because the request fails if the specified date is invalid. So currently I'm doing something like this:
SELECT * FROM table WHERE date IN BETWEEN '2012-01-01' AND 'VALID_MONTH_END_DATE' (VALID Query)
I want to know if there is a way to avoid this correct calculation of the end date?
Explanation
I thought above the first day of the next month, but even then I will have to apply some logic if it is December, the next month will be January next year. I wanted to know if a SQL solution is possible?
source share