I have a quick question regarding selecting dateranges in MySQL, formatted like this YYYY-MM-DD
I read MySQL select date range issue and understand the basic usage of it, but how can I “include” the 29th of february for example? I’d like to avoid a PHP workaround, is there anything like that in MySQL?
I can’t quite understand http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
can someone give me an example how to select last years data from february?
The moment you specify a month, MYSQL engine is smart enough to get the correct number of days for a month (whether it’s a leap year or not) and evaluate your date fields accordingly.
To get data between two dates of your choice: (using
interval -1 yearto get one year back from today)Or you can simply just write with YEAR function without
BETWEENFor the question on
Str_to_Datethe function is used to convert string that contains a date into a date type with a desired format and it’s the inverse ofDATE_FORMATfunction.Notice in the above demo that you have to specify the day, month, year arrangement in the format you are adding into the
STR_TO_DATEfunction.Reference on MYSQL Site
You may also look into
DATE_FORMATto format a date type field into a desired date format.PS: it’s rather vague what you are really tryhing to achieve here, without sameple data and expected reslts shown in the question.. 🙂