Sorting dates stored as varchar/rfc822 - mysql

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.

Related

MySQL Date Format From Fri Jan 10 00:00:00 UTC 1992 to YYYY-MM-DD

I have a data "Fri Jan 10 00:00:00 UTC 1992" And I want to convert it to 1992-10-01.
I know how to do it with Javascript, but I have no idea how to do it with MySQL
I tried :
DATE_FORMAT(column, '%d/%m/%Y')
But it's return null
select date_format(str_to_date("Fri Jan 10 00:00:00 UTC 1992","%a %b %d %H:%i:%s UTC %Y"),"%Y-%m-%d");
First you need str_to_date, using "%a %b %d %H:%i:%s UTC %Y" to get a date, after that you can format the date using date_format in the desired format.
These specifiers shown in the following table may be used in the format string.

How to convert string with UTC timediff to mysql datetime

I am having application that fetches data from RSS Feed. Feed item contains pubDate which is of format Mon, 26 Feb 2018 03:00:00 -0800 like this. I need to save it in mysql database. I am trying to convert the string to datetime using str_to_date function. I don't know what exactly -0800 is. Here is my code
select str_to_date('Mon, 26 Feb 2018 03:00:00 -0800', '%W, %e %b %Y %H:%i:%s %T') ;
Its is giving me null, as the last value is not converted. What exactly i have to update with %T
Just ignore the -0800 and run you get the required result.output after removing the -0800

MySQL date format convert "Fri, 06 Nov 2015 04:06:05 -0500"

I have "Fri, 06 Nov 2015 04:06:05 -0500" date format in MySQL.
I want to convert it to Y-m-d H:i:s format within MySQL query. Can anyone please help?
I tried this DATE_FORMAT("Fri, 06 Nov 2015 04:06:05 -0500", '%Y-%m-%d H:i:s') but didn't work.
You need to make two transformations here, to cover the general case. First, you need to convert your non standard timestamp into a date, and then you need to format that date in the output you want. Diagramatically:
your timestamp string -> MySQL date -> some other string output
Consider this query:
SELECT
DATE_FORMAT(STR_TO_DATE('Fri, 06 Nov 2015 04:06:05 -0500', '%a, %d %b %Y %H:%i:%S'),
'%Y-%m-%d %H:%i:%s') AS output
FROM dual;
Demo
You can use strtotime() in php..it looks likes.
date('Y-m-d H:i:s', strtotime($row['col_name']));
You can try this
<?php
echo date('Y-m-d H:i:s', strtotime("Fri, 06 Nov 2015 04:06:05 -0500"));
?>
Note- In strtotime() funtion you can pass your date value, which you wnats to convert.
Hope this will help!
DATE_FORMAT doesn't recognize the string you passed as a date, you have to use STR_TO_DATE() and match the string as follows:
STR_TO_DATE("Fri, 06 Nov 2015 04:06:05 -0500",'%a, %d %b %Y %H:%i:%s');
This already outputs in Y-m-d H:i:s format.

Importing CSV file - mysql datatype for date

I'm having an issue with importing some data that contains a date / time field which looks like:
Wed Apr 08 15:11:50 UTC 2015
When I import this I get "0000-00-00 00:00:00".
Is there something I can do to import this field into a datatype of datetime? Do I need to do some sort of convert process so the data will be in the correct format?
Thanks in advance.
Stu
mysql> SELECT STR_TO_DATE('Wed Apr 08 15:11:50 UTC 2015', '%a %M %d %H:%i:%s UTC %Y');
+-------------------------------------------------------------------------+
| STR_TO_DATE('Wed Apr 08 15:11:50 UTC 2015', '%a %M %d %H:%i:%s UTC %Y') |
+-------------------------------------------------------------------------+
| 2015-04-08 15:11:50 |
+-------------------------------------------------------------------------+
1 row in set (0.00 sec)
Depending on how you are importing, you may need to first put it into an #variable, the convert using real_col_name = STR_TO_DATE(#variable, '%a...')

How to convert DateTime datatype of java in to DATE in mysql

I have a date like:-
Thu Dec 26 05:24:05 UTC 2013
and I want to convert this date like this
26th Dec 2013
I want solution for it.
The date you have is a string and in mysql you need to use str_to_date() in conjunction with date_format() to get the date in a formatted pattern
mysql> select
date_format(
str_to_date(
'Thu Dec 26 05:24:05 UTC 2013','%a %b %d %h:%i:%s UTC %Y'
),'%D %b %Y'
) as date;
+---------------+
| date |
+---------------+
| 26th Dec 2013 |
+---------------+
From a date object, you can individually extract date.
Date dte=new Date();
dte.format(dd.MM..YYYY);
for more information, visit http://www.tutorialspoint.com/java/java_date_time.htm