Parsing Dates for from CSV for SQL - mysql

I am using the LOAD DATA LOCAL INFILE function to pull log data from a csv into a mysql DB so I can use it in Rails.
Using the answer to this question:
Modify CSV field on import to mysql
The only issue I have is how to parse my date time field.
Thu Mar 01 11:49:16 +0000 2012
STR_TO_DATE(#csvdate, '??????????')

Try to use STR_TO_DATE function. For example -
SET #date = 'Thu Mar 01 11:49:16 2012';
SELECT STR_TO_DATE(#date, '%a %b %d %H:%i:%S %Y');
STR_TO_DATE function.

Related

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() issue

I have date on MySQL table on the format of Fri Oct 30 09:50:37 2015, when I try to format using DATE_FORMAT(delv_time,'%Y-%m-%d') it return null.
That's not as date as far as MySQL is concerned. That's a string. You need to convert it into a date using STR_TO_DATE(). Then you can use DATE_FORMAT() to get only the date portion of the datetime.
DATE_FORMAT(STR_TO_DATE(delv_time,'%a %b %d %T %Y'),'%Y-%m-%d')

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...')

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.

Convert a date string into proper date stamp in MYSQL (I need the sql code, not php)

I have a huge spreadsheet with four date columns for the details of various orders that I'm importing from one system to another.
The dates currently look like this in the MYSQL database I created from the spreadsheet:
Sun Nov 27 17:02:40 PST 2011
Sat Nov 26 17:01:54 PST 2011
Mon Nov 28 18:06:29 PST 2011
and I want them to be simply a date stamp that I can query for running reports against in MYSQL. I'm not using PHP right now and I want to be able to do this conversion in MySQL. I've found similar threads, but nothing that was helpful for this particular approach. Apologies if it's been addressed somewhere already.
Use str_to_date
select str_to_date('Sun Nov 27 17:02:40 PST 2011', '%a %b %e %H:%i:%s PST %y')
I don't think you can convert the timezone name properly though.