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.
Related
I'm trying to convert a datetime field, which is set to EST, to a UNIX timestamp. But the catch is, the timestamp should be set to midnight UTC.
For example,
May 1 2015 would be 1430438400000 (01 May 2015 00:00:00 UTC).
I tried converting first the datetime to UTC, then format it to a 24-hour format, but apparently it didn't work for me. Code below:
UNIX_TIMESTAMP(DATE_FORMAT(DATE(DATE_ADD(rv.created_at,INTERVAL 4 HOUR)),'%Y-%m-%d 00:00:00'))
Can anyone help? Thanks.
UPDATE
I finally got the answer. Putting it here for future reference
FLOOR(UNIX_TIMESTAMP(DATE_SUB(DATE_FORMAT(DATE(rv.created_at),'%Y-%m-%d 00:00:00'),INTERVAL 4 HOUR))*1000)
Unless I'm not understanding your question this works:
UNIX_TIMESTAMP(DATE_FORMAT(rv.created_at,'%Y-%m-%d 00:00:00'))
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 need to manipulate my database. I'm stuck at datetime field. For example, it is 08-Apr-2015 21:13:49 and SQL translate it into 1428527629. How SQL translate the d-M-Y H:i:s format?
1428527629 unix timestamp, stands for seconds since Jan 01 1970. (UTC)
You have several SQL functions to work with dates, such as: FROM_UNIXTIME(1428527629)
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 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