Convert unconventional string to mysql datetime in MySQL - 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.

Related

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

MYSQL Date Range RFC 2822

THE SITUATION
I pull timestamps formatted in RFC 2822 (Sat, 01 Dec 2012 05:49:45 +0000) and store them in a VARCHAR field in MYSQL. I have a start_date and end_date.
THE GOAL
Search BETWEEN two dates (like start_date BETWEEN '2012-11-01' AND '2012-12-01')
THE CONDITIONS
I want to do this with pure SQL and not do post processing in PHP
THE ACCEPTABLE COMPROMISE
I don't want to, but I will convert and store them as DATETIME by using PHP if needed.
Can anyone help me accomplish my goal (listed above).
Rick
You could convert your string dates do datetime using str_to_date:
select str_to_date('Sat, 01 Dec 2012 05:49:45 +0000','%a, %d %b %Y %T')
If you need to convert also timezone, try this:
set #datestring='Sat, 01 Dec 2012 05:49:45 +0000';
select
CONVERT_TZ(
str_to_date(#datestring,'%a, %d %b %Y %T'),
concat(mid(#datestring, 27, 3), ':', mid(#datestring, 30, 2)),
'+00:00'
)
Store them as a native DATETIME. This is the only sane approach.
Why are you so opposed to using the proper tool for the job?
Storing timestamps as a string is poor use of the database features. Since they were all the same format, they could have easily been converted to a datetime on input.

Convert VARCHAR timestamp to TIMESTAMP?

I have a time stamp of the form "17:16:28 Sep 13, 2011 PDT" in a MySQL database. The type of the field in the database is VARCHAR. I would like to convert this VARCHAR to a field of type TIMESTAMP in MySQL.
I tried a few solutions suggested elsewhere on this site, such as using a string_to_time function, but these solutions all start from a different type of timestamp.
How do I convert the VARCHAR timestamp mentioned above to a TIMESTAMP recognised by MySQL, in such a way that I can sort my data by date?
You can do this by using STR_TO_DATE function in MySQL, try this:
SELECT STR_TO_DATE("17:16:28 Sep 13, 2011 PDT", '%H:%i:%s %b %d, %Y PDT');
EDIT:
SELECT STR_TO_DATE(field_name, '%H:%i:%s %b %d, %Y PDT') AS new_time
FROM table_name;
Use str_to_date with formatting:
select unix_timestamp(str_to_date("17:16:28 Sep 13, 2011 PDT","%T %b %d, %Y PDT"));
If the day part (e.g.: 13) is not represented as 01, 02..etc. but 1, 2, 3 when it's only one digit, change the %d to %e
Be careful because the timezone will not be recognized, it's only a string literal for the formatting! If you have different timezones for different records you should use the convert_tz() function to get the proper timestamp.

MySQL convert date string to Unix timestamp

How do I convert the following format to unix timestamp?
Apr 15 2012 12:00AM
The format I get from DB seems to have AM at the end.
I've tried using the following but it did not work:
CONVERT(DATETIME, Sales.SalesDate, 103) AS DTSALESDATE,
CONVERT(TIMESTAMP, Sales.SalesDate, 103) AS TSSALESDATE
where Sales.SalesDate value is Apr 15 2012 12:00AM
Here's an example of how to convert DATETIME to UNIX timestamp:
SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p'))
Here's an example of how to change date format:
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p')),'%m-%d-%Y %h:%i:%p')
Documentation: UNIX_TIMESTAMP, FROM_UNIXTIME
You will certainly have to use both STR_TO_DATE to convert your date to a MySQL standard date format, and UNIX_TIMESTAMP to get the timestamp from it.
Given the format of your date, something like
UNIX_TIMESTAMP(STR_TO_DATE(Sales.SalesDate, '%M %e %Y %h:%i%p'))
Will gives you a valid timestamp. Look the STR_TO_DATE documentation to have more information on the format string.
If you want to create a timestamp as returned by java's Date.getTime() you should multiply by 1000.
SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p'))*1000
Now for a more standard date format use:
SELECT UNIX_TIMESTAMP(STR_TO_DATE('2022-12-14 20:58:00', '%Y-%m-%d %H:%i:%s'))*1000
From http://www.epochconverter.com/
SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())
My bad, SELECT unix_timestamp(time) Time format: YYYY-MM-DD HH:MM:SS or YYMMDD or YYYYMMDD. More on using timestamps with MySQL:
http://www.epochconverter.com/programming/mysql-from-unixtime.php

MySQL:How to sort date with RFC112 format?

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