how to do this right
Select DATE_FORMAT(EventDate, '%b %d, %Y')
from details
Order by STR_TO_DATE(EventDate, '%d-%m-%y')
sample records,
Oct 24, 2012
Oct 27, 2012
Oct 28, 2012
Oct 20, 2012
Dec 22, 2012
Jan 11, 2013
Jan 19, 2013
Nov 24, 2012
Dec 29, 2012
What about this:
Select DATE_FORMAT(EventDate, '%b %d, %Y') from details Order by EventDate
Order by STR_TO_DATE(EventDate, '%d-%m-%y')
So we start with EventDate which you say is a DATE and we feed it as first argument to STR_TO_DATE(). Such function expects a string, so MySQL casts your date as string possibly using the default format (YYYY-MM-DD). Then it tries to cast the string to date using DD-MM-YYYY as format but your string is not in that format. Do you understand where I want to go?
To sum up: you don't need to cast a date to date--it's already a date!
Order by EventDate
Related
I have usages table which contains field values like below
id | page_open_time
1, 28 May 2019 08:39:11
2, 12 July 2019 18:39:11
3, 22 December 2019 11:39:11
4, 23 June 2019 12:39:11
Now I would like to get all records where page_open_time is greater than 1 july, 2019. For this, I wrote this query below , but it didnt help. Is there any way, I can perform this ?
SELECT * FROM `usages` where page_open_time > '2019-07-01'
Your dates are not in a valid MySQL DATETIME format, so you need to convert them before comparison, using STR_TO_DATE:
SELECT *
FROM usages
WHERE STR_TO_DATE(page_open_time, '%d %M %Y %H:%i:%s') > '2019-07-01'
Output
id page_open_time
2 12 July 2019 18:39:11
3 22 December 2019 11:39:11
Demo on dbfiddle
I was trying to sort the data in-order by date but it doesn't sort perfectly. I don't know if there's an error in the code.
Here's some of the data in character in my database:
March 5, 2016
March 17, 2016
August 9, 2017
April 2, 2016
July 5,2018
January 15, 2019
The query I used is:
SELECT * FROM reporting ORDER BY date ASC
Result are:
April 2, 2016
August 9, 2017
January 15, 2019
July 5,2018
March 5, 2016
March 17, 2016
Expected Result:
March 5, 2016
March 17, 2016
April 2, 2016
August 9, 2017
July 5,2018
January 15, 2019
use str_to_date() to convert your date field from string to datetime
SELECT * FROM reporting ORDER BY str_to_date(`date`,'%M %d, %Y') ASC
use date format
ORDER BY DATE_FORMAT(date, "%F-%d-%Y") as date ASC
You may try this,
For SQL Server:
convert( varchar, [date], 120) will convert your date in yyyy-MM-dd format.
select * from reporting order by convert( varchar, [date], 120) asc
For MySQL:
select * from reporting order by DATE_FORMAT(date, "%Y-%m-%e") asc
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
I have been recording Twitter data for a project I'm working on the date information is saved as Thu, 14 Jul 2011 06:21:48 +0000 in a string field.
How do I select data that falls between two dated using mySQL? I can get data larger than a value or smaller than a value but not between to values.
The data for example is:
Thu, 14 Jul 2011 06:21:48 +0000
Thu, 14 Jul 2011 12:18:21 +0000
Thu, 14 Jul 2011 18:48:00 +0000
Thu, 14 Jul 2011 23:48:02 +0000
Fri, 15 Jul 2011 06:48:10 +0000
Fri, 15 Jul 2011 12:48:00 +0000
Fri, 15 Jul 2011 18:43:32 +0000
Fri, 15 Jul 2011 23:44:08 +0000
Sat, 16 Jul 2011 06:47:08 +0000
Sat, 16 Jul 2011 12:46:49 +0000
Sat, 16 Jul 2011 18:45:41 +0000
Sat, 16 Jul 2011 23:41:27 +0000
My SQL string is:
SELECT *
FROM twitter
WHERE SUBSTR(twitter_date, 6, 11) >= '2011-06-15'
AND SUBSTR(twitter_date, 6, 11) <= '2011-06-21'
I've tried BETWEEN statements as well but no luck.
Any help will be appreciated!
You can't use between because they are strings and not actual date fields. If you know the format of the date will always be the same, you can do the following:
STR_TO_DATE(str,format)
SELECT *
FROM twitter
WHERE STR_TO_DATE(twitter_date, '%a, %c %b %Y %k:%i:%s')
between '2011-06-15' AND '2011-06-21'
You are comparing "6 Jul 2011" with 2011-06-15.
It should be
SELECT *
FROM twitter
WHERE SUBSTR(twitter_date, 6, 11) >= '6 Jul 2011'
AND SUBSTR(twitter_date, 6, 11) <= '21 Jul 2011'
You may want to look into MySQLs STR_TO_DATE function. This will give you a date and between should work as expected.
The query will fail. You're comparing two STRINGS, not dates. You need to tell MySQL explicitly that you want to treat the strings as dates. Since it's date information, store it in a date or datetime field type in MySQL, which saves you all this trouble:
SELECT STR_TO_DATE(SUBSTR(twitter_date, 6, 11)) >= '2011-06-15' ...
Forcing your left-hand-side to be a proper date value will hint to MySQL that it should treat the right-hand-side as a date as well.
Once you have altered your table to hold the date data in proper DATETIME fields, you may want to refer to my answer to the following question regarding dates and filtering them, which may prove useful.
For example, in your query / data above, what would happen if you had no data for one of the dates you were querying? You'd get no output for that date, which may look odd when charting, etc. If you use the technique I describe in the attached question, you can get that additional information, and other useful stuff like the day of the week, month name, etc.
Select all months within given date span, including the ones with 0 values
Hope that helps!
Don't ask why (it's something out of my control), but dates are being stored as RFC-822 in our MySQL DB in varchar(125).
RFC-822 = Mon Jun 13 2011 11:30:00 GMT-0400 (EDT) or Mon Jun 13 17:00:00 EDT 2011
Is there a way I can sort by date in that format or at the very least, pull the date out as YYYYMMDD or Unix time?
Some voodoo can help with the first format:
SET #dt = 'Mon Jun 13 2011 11:30:00 GMT-0400 (EDT)';
SELECT
CONVERT_TZ(
-- Parse all, but timezone
STR_TO_DATE(#dt, '%a %b %e %Y %H:%i:%s'),
-- Parse timezone to '+NN:NN' format
INSERT(SUBSTRING_INDEX(SUBSTRING_INDEX(#dt, 'GMT', -1), ' ', 1), 4, 0, ':'),
-- Our unified timezone
'+00:00'
);
-- Result: 2011-06-13 15:30:00
CONVERT_TZ supports EDT-like abbreviations too, but not everywhere.