I'm importing an rss feed into MySQL 5.1 via wget and LOAD DATA INFILE.
This is all working well, but, I'm having problems converting the date & time in the rss feed to a datetime col in mysql.
An example date from the feed is:
Sat, 19 Jan 2013 11:10:19 GMT
Any ideas how I can cast or convert this?
Thanks
J.
How about this? using str_to_Date:
STR_TO_DATE('rss_date', '%y-%m-%d')
use STR_TO_DATE
SELECT STR_TO_DATE(DATE_STRING,'%a, %d %b %Y %h:%i:%s')
SQLFiddle Demo
OTHER SOURCE
STR_TO_DATE
Date Formats
Related
Currently, we are trying to convert: 'Wed, 13 Jun 2018 09:34:36' format to MySQL datetime format.
What would be a good approach for this? We have tried cast()
I ended up using this:
select STR_TO_DATE('13-Jun-2018 09:34:36', '%d-%M-%Y %h:%i:%s')
In python, I removed the first part and ending that was not needed.
I am querying MySQL database and getting date time in 2016-12-28 14:14:00 UTC format.
But I want my date time converted to the format like 12/06/2016 11:28 PM in rails.
You can use strftime to get the format as below:
d = '2016-12-28 14:14:00 UTC'.to_time
d.strftime('%m/%d/%Y %H:%M%p')
For Rails project I usually create special file called config/initializers/date_time_formats.rb
And add to their predefined a time formats.
Time::DATE_FORMATS[:human_with_12hours] = '%m/%d/%Y %I:%M %p'
Reload server and use it like this: YOUR_DATE_VARIABLE.to_s(:human_with_12hours)
You can create lot of time and date formats in this file.
I'm on this page now reading documentation...
Data time function
for this
SELECT TIME_FORMAT("9:45 PM", "%H:%i:%s")
Result what I get
09:45:00
I think what should I get is
21:45:00
How to achieve this? I am on the documentation page but not getting anything.
Maybe I should say how do I read the string "9:45 PM" into time and make it in a time and then convert it to what I want?
Try:
SELECT STR_TO_DATE('09:45 PM', '%h:%i %p');
Or
SELECT TIME_FORMAT(STR_TO_DATE('09:45 PM', '%h:%i %p'), '%T');
SQL Fiddle demo
Check:
STR_TO_DATE
TIME_FORMAT
try with this
use DATE_FORMAT()
and use %T to get 24 hours format......hope this link will be usefull for you http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format
Refer date function
$RetStr = date("H:i:s", strtotime($time));
I am not familiar with timestamp format much.
I have a text for example
'Jul 19, 2013 12:00 pm'
I want to store it to mysql. What format of this timestamp is in MySQL and how should I format it properly in perl before passing it to mysql.
Thanks.
What is the format of timestamps in MySQL?
I highly recommend that you read the MySQL manual. You'll get your answers much faster than by posting a question on StackOverflow. From the docs:
TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
As you can see, the format is YYYY-MM-DD HH:MM:SS.
As ysth points out in the comments, MySQL also has a DATETIME data type:
MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
Several things to note:
DATETIME and TIMESTAMP both use the format YYYY-MM-DD HH:MM:SS
The range of dates supported by DATETIME is much larger than the range for TIMESTAMP
TIMESTAMP converts values to UTC for storage and back to the local time zone on retrieval; DATETIME does no time zone conversion
If you aren't already wedded to the TIMESTAMP data type you might consider using DATETIME instead, depending on what kind of data you're trying to store. See this StackOverflow question for more details on DATETIME vs. TIMESTAMP.
How should I format it in Perl before passing it to MySQL?
To convert date/time strings to different formats in Perl, you can use the core (since v5.9) module Time::Piece:
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Time::Piece;
my $date = 'Jul 19, 2013 12:00 pm';
my $t = Time::Piece->strptime($date, '%b %d, %Y %I:%M %p');
say $t->strftime('%F %T');
# 2013-07-19 12:00:00
It's not clear from your example date string whether the day and hour are zero-padded. The above example assumes a format like
Oct 01, 2013 05:00 am
where days and hours less than ten begin with a zero. If your input format is actually
Oct 1, 2013 5:00 am
then you need to change the format string passed to Time::Piece->strptime. A list of format specifiers can be found in the man page for strftime.
Time::Piece has been a Perl core module since 5.9. It provides a convenient way to parse any time format input (with strptime()) and produce a differently formatted output (with strftime().
#cat ./tconv
#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
my $t = Time::Piece->strptime( shift, "%b %d, %Y %I:%M %p" );
print $t->epoch, "\n";
print $t->strftime( $t, "%Y/%m/%d %T %z" ), "\n";
#./tconv "Jul 19, 2013 12:00 pm"
1374235200
Fri Jul 19 12:00:00 2013
I'm using the date plugin for Template::Toolkit (Template::Plugin::Date), it works well with datetimes (yyyy-mm-dd hh:mm:ss) pulled straight out of MySQL, but it will not work with dates (yyyy-mm-dd).
What's the simplest way to get date.format to accept dates (without modifying the sql query)?
Thanks.
[% date.format(yourDateColumn _ ' 00:00:00', '%d %b %Y') %]