Trying to select data between middle of two years and months - mysql

I'm trying to select data between july 2017 to jun 2018 using sql query but not getting expected output. Let's say m having data from jan 2017 up to dec 2018,
and i want to select data between july 2017 to jun 2018. how can I achieve this?
This is the data i'm having
And Expected Output is

See MySQL STR_TO_DATE() Function
SELECT * FROM <YOUR TABLE> WHERE STR_TO_DATE(CONCAT('1 ',`Month`,' ',`Year`), '%d %M %Y')
BETWEEN STR_TO_DATE('1 Jun 2017', '%d %b %Y') AND STR_TO_DATE('31 Jul 2017', '%d %b %Y');

Related

How to fix group by and order by year and months in sql?

I'm trying to fix the order by and group by year and month format. This is my sql query
SELECT DATE_FORMAT(created_at, "%Y %b") ym
FROM `book_summaries`
GROUP BY DATE_FORMAT(created_at, "%Y %b")
ORDER BY DATE_FORMAT(created_at, "%Y %b")
But the sql query result is this:
2018 Apr
2018 Aug
2018 Dec
2018 Feb
2018 Jul
2018 Jun
2018 Mar
2018 May
2018 Nov
2018 Oct
2018 Sep
2019 Apr
2019 Aug
2019 Dec
It seems that only the year format has been ordered but not the months in it. How can I alter my query to order by year and then months?
You can order by the minimum date in each range:
SELECT DATE_FORMAT(created_at, '%Y %b') ym
FROM `book_summaries`
GROUP BY DATE_FORMAT(created_at, '%Y %b')
ORDER BY MIN(created_at);
Note that I also replaced the double quotes with single quotes. Single quotes are the SQL standard for strings (although some database do support double quotes as a string delimiter).

MySql Convert Date from STR_TO_DATE date_format

How do you convert the following date Mar 7 2017 1:26:46:886AM to 2017-03-07 01:26:00
select DATE_FORMAT(STR_TO_DATE('Mar 7 2017 1:26:46:886AM', '%b-%d-%Y'), '%Y-%m-%d');
I keep getting null
This is the answer
SELECT STR_TO_DATE("Mar 7 2017 1:26:46:886AM", "%M %d %Y %H:%i:%S")
you could use
select STR_TO_DATE('Mar 7 2017 1:26:46:886AM', '%b %d %Y %h:%i:%s%f' )

Order dates in MySQL with this format "01 August 2013"

how to order dates in ASC,
Dates are in the following format
01 January 2013
13 August 2013
27 June 2013
I've tried this query
SELECT * FROM table ORDER BY TIMESTAMP(date) ASC
and this query
SELECT * FROM property_rent ORDER BY UNIX_TIMESTAMP(date) ASC
Using the str_to_date function:
SELECT * FROM table ORDER BY str_to_date(date, 'your format here')
e.g.
SELECT * FROM table ORDER BY str_to_date(date, '%d %M %Y')
or similar.

What is wrong in select str_to_date ( '%e %b %Y','14 Aug 1987' )

What is wrong in
select str_to_date ( '%e %b %Y','14 Aug 1987' )
I even tried '%d %b %Y'
Referred http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
because it is "STRING" to "DATE" ... put the string first ...
select str_to_date ( '14 Aug 1987','%e %b %Y' )

mysql date format with changing string value

I have a field called Timestamp, that stores its values as text as opposed to an actual Timestamp. The logging application is unchangeable, unfortunately.
So
table.Timestamp -> text field with format -> "Wed Mar 02 13:28:59 CDT 2011"
I have been developing a query to purge all but the most recent row using this as my Timestamp selector, which is also converting the string into a date ->
MAX( STR_To_DATE( table.Timestamp , '%a %b %d %H:%i:%s CDT %Y' )
My query works perfectly...
However, what I've found is that the string value -> 'CDT' -> changes between 'CDT' and 'CST' depending on whether the current time is daylight savings time or not. During daylight savings time, it logs as 'CDT', and vice versa.
So all the rows that contain 'CST' get ignored when I run this ->
MAX( STR_To_DATE( table.Timestamp , '%a %b %d %H:%i:%s CDT %Y' )
and all the rows that contain 'CDT' get ignored when I run this ->
MAX( STR_To_DATE( table.Timestamp , '%a %b %d %H:%i:%s CST %Y' )
Is there a way to make it run against both string formats?
EDIT -
So given the following data (different only by 'CDT and 'CST':
"Wed Mar 02 13:28:59 CDT 2011" and "Tue Mar 08 09:42:07 CST 2011"
Currently it will return as follows:
MAX( STR_To_DATE( table.Timestamp , '%a %b %d %H:%i:%s CDT %Y' ) Returns - Wed Mar 02 13:28:59 CDT 2011
MAX( STR_To_DATE( table.Timestamp , '%a %b %d %H:%i:%s CST %Y' ) Returns - Tue Mar 08 09:42:07 CST 2011
I want it to return "Tue Mar 08 09:42:07 CST 2011" no matter what.
EDIT 2:
This is my main query:
DELETE
FROM `table` main
WHERE STR_To_DATE( main.Timestamp , '%a %b %d %H:%i:%s CST %Y' ) <
(SELECT COALESCE(STR_To_DATE( sub.Timestamp , '%a %b %d %H:%i:%s CDT %Y' ), STR_To_DATE( sub.Timestamp , '%a %b %d %H:%i:%s CST %Y' ))
FROM `table` sub
WHERE sub.Retrieving_User = main.Retrieving_User )
Coalesce the two. COALESCE means to return the first non-null value in the set.
In this case it will first try to parse it using "CDT". If that fails, returning NULL, it will try to parse using "CST" instead. (If that fails, it will reach the end of the coalesce list and just return NULL, but in your case it won't get that far).
select MAX( COALESCE(STR_To_DATE( test.Timestamp , '%a %b %d %H:%i:%s CDT %Y' ), STR_To_DATE( test.Timestamp , '%a %b %d %H:%i:%s CST %Y' )))
from test
SQL Fiddle Try changing the data in the table and see it work.