MySQL:How to sort date with RFC112 format? - mysql

For example, I have a column named date on my table post and I want to sort it in ascending.
On my date column I fill it with RCF112 format, eg: Sun, 22 APR 2012 5:21:22.
First I begin with this command:
SELECT *
FROM post
ORDER BY date ASC
But the result appears to be incorrect because it was sorted according to its string, eg. the Sun, 15 APR 2012 will be older than Wed,11 APR 2012 because "Sun" starts with 'S' which is in alphabetic ahead 'W', so the "Sun, 15 APR 2012" appears first.
How to correct this command?

You need to parse the string as datetime to be able to sort it correctly.
Using your format, you can try something like this:
STR_TO_DATE('Sun, 22 APR 2012 5:21:22', '%a, %e %b %Y %h:%i:%S')
which creates the date 2012-04-22 05:21:22.
So, your query should look something like this:
SELECT *
FROM post
ORDER BY
STR_TO_DATE(date, '%a, %e %b %Y %h:%i:%S')
ASC
As others might have already suggested, you could use the datetime field type and format the date in the select (date_format http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format) to fit your requirements.

SELECT *
FROM post
ORDER BY STR_TO_DATE(datestr, '%a, %d %b %Y %T') ASC

Related

convert full date string to date mysql

I have database which contains string like this
22 Jan 2019 11:03
I would like to convert this string to date so I apply this query
select DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d-%m-%Y') ,'%d-%m-%Y');
but I get a null result
All you have to do is change small letter m to big letter M in your str_to_date function.
select STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y');
so the final query would be:
select DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y') ,'%d-%m-%Y');
Here is a demo
SELECT STR_TO_DATE('22 Jan 2019 11:03','%d,%m,%Y');
Basically you first need to understand the values to pass in STR_TO_DATE() and DATE_FORMAT() functions
STR_TO_DATE(my date string , current format of my date string)
Now what you are not doing right is that in STR_TO_DATE() you are passing the format '%d-%m-%Y' , this format says that the input string has hyphen separated date month and year values which is not true.
Now in your case the actual format of your date string is the following
'%d %M %Y %h:%i'
STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y %h:%i')
%d - day
%M - month
%Y - year
%h - hour
%i - minute
For more info on formats click
Now that we have a complete valid value from string to date including hour and minutes, we can convert this date into any of our required format using correct parameters
SELECT DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y %h:%i') ,'%d-%m-%y %h:%i');
You can try out various examples here
https://www.mysqltutorial.org/tryit/query/mysql-str_to_date/#1

SQL: Get the beginning date of the month from a Month_year name column

I have a date data as below:
Monthyear
Jan 2018
Feb 2018
Mar 2018
I want to convert this into this format:
Monthyear
01-01-2018
01-02-2018
01-03-2018
That is, as a date with the first date of the month.
I tried casting the above and it doesnt work.
Kindly help me out.
You can use STR_TO_DATE:
SELECT STR_TO_DATE(CONCAT('1 ', Monthyear), '%e %b %Y')
e.g.
SELECT STR_TO_DATE(CONCAT('1 ', 'Jan 2018'), '%e %b %Y')
Output:
2018-01-01
The reason for CONCATing a '1 ' to the string is to avoid issues with SQL NO_ZERO_DATE mode (this is discussed in the manual entry for STR_TO_DATE).
use str_to_date()
select str_to_Date(concat('01',Monthyear),"%d %b %Y")

Mysql between code is not showing the correct output

I have written a query to get the data between a particular month and year with another month and year.
For example I want to get data between Nov 2014 to Nov 2015.
However, my query is just showing the output for just Nov 2014 and Nov 2015.
The data in between this two dates is not showing. Below is my query:
SELECT * FROM prc.tbictrepairsustainability
WHERE sustainRegion = 'America'
AND (DATE_FORMAT(sustainDate, '%m %Y') BETWEEN '11 2014' AND '11 2015') ;
My sustainDate column data type is date.
Pls correct me if there is something wrong with my query.
Thanks
It worked after i changed the date format to '%d %m %Y' with STR_TO_DATE.
SELECT *
FROM prc.tbictrepairsustainability
WHERE sustainRegion = 'America'
AND sustainDate Between STR_TO_DATE('01 11 2014','%d %m %Y')
AND STR_TO_DATE('30 11 2015','%d %m %Y')
You need to convert the strings to dates in order to compare them to the date in the table.
SELECT *
FROM prc.tbictrepairsustainability
WHERE sustainRegion = 'America'
AND (sustainDate BETWEEN STR_TO_DATE('30 11 2014','%d %m %Y')
AND STR_TO_DATE('30 11 2015','%d %m %Y'))
Updated per Ronald's suggestion.

Select records where datetime is greater than the specified date

Problem
I am trying to fetch all the records from table where date_time field is greater than 'Thu, 11 Jul 2013' by running the below mentioned query. Value in the date_time field is stored in this format => Thu, 11 Jul 2013 08:29:37. Any help will be great.
Datatype of field date_time is varchar
Query
SELECT * FROM table_name
WHERE username = 'mark#example.com'
AND STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= 'Thu, 11 Jul 2013 00:00:00';
Here is yet another great example of why you should implement date/time fields in MySQL using the date, datetime, or timestamp field types and let your application deal with how to format the date for output.
Right now you are going to need to do something like:
SELECT * FROM table_name
WHERE username = 'mark#example.com'
AND STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= STR_TO_DATE('Thu, 11 Jul 2013 00:00:00', '%a, %e %b %Y %H:%i:%s');
This query will not be able to use any index you have on your date_time field, so the query will be very inefficient. It will need to perform a full table scan, converting the value of each row in order to make the comparison.
What you should be doing is:
SELECT * FROM table_name
WHERE username = 'mark#example.com'
AND date_time >= STR_TO_DATE('Thu, 11 Jul 2013 00:00:00', '%a, %e %b %Y %H:%i:%s');
Here if you have your field in the MySQL datetime format, you just need to convert the input to a this format for matching. Since your field data is already in this format, you will be able to utilize an index for the search.
You are trying to compare a date with a String.
The str_to_date function application is correct, but you are not comparing to a date.
The right way to do it is:
select * from yourTable
where STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= '2013-07-11 00:00:00'
Notice that the date format is YYYY-MM-DD HH:mm:ss (which is MySQL default date format).
Of course, you can also compare to str_to_date results:
... where STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= STR_TO_DATE('Thu, 11 Jul 2013 00:00:00', '%a, %e %b %Y %H:%i:%s')

Convert unconventional string to mysql datetime in MySQL

I want to convert a field that represents a date-time but is currently a VARCHAR to a DATETIME field.
Currently the representation looks like: 'Sun May 20 01:04:39 +0000 2012'
I want to do this operation in a query.
Use STR_TO_DATE
SELECT STR_TO_DATE('Sun May 20 01:04:39 +0000 2012', '%a %M %d %H:%i:%S +0000 %Y')
SQLFiddle Demo
Date Format
Use the DATE function.
This does the job and can be used inside queries.