MYSQL Date Range RFC 2822 - mysql

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.

Related

Converting multiple datetime formats to single mysql datetime format

I hired a freelancer a little while ago to parse a website which had a datetime field. I had to put the project on the back burner shortly after the freelancer completed and now that I'm getting back into it, I've noticed some issues when I go into the MySQL database.
Specifically, there are two different datetime formats and I can't figure out how to update them into a unified MySQL sortable datetime field
Apr 8 - 2:23 AM <- for current year updates
Tue, Dec 2, 2014 06:06:00 PM <- for all previous year updates
2014-12-02 06:06:00PM <- desired format
I have a unique id in the table so I can select and update the formats easily. All "Apr 8 - 2:23 AM" format is < '6340' for example. I also have created a "date_proper" column to update the current date column.
I just can't for the life of me figure out the correct code to update the different formats into the same unified format. Any help would be much appreciated.
You need to use str_to_date() for this. First, fix the date_proper column so it is a datetime. Formats should be handled on input and output. The proper storage for dates is using native formats:
alter table modify date_proper datetime;
Then you can update the values:
update t
date_proper = (case when format < '6340'
then str_to_date(concat(year(now), ' ', col), '%Y %b %d - %h:%i %p'
else str_to_date(substr(col, 5), '%b %d, %Y %h:%i:%s %p'
end);

MYSQL query: select between date with local format

my mysql database tb_date (varchar 20):
16 November 2014
06 December 2014
01 April 2014
12 April 2015
I want select between 01 January 2014 until 31 December 2014, how the query is with date conversion?
thanks..
This is an anti-pattern, storing date values in VARCHAR columns, rather than using datatypes specifically designed and implemented for storing date values... DATE, DATETIME or TIMESTAMP.
To answer your question, before it gets closed, you could use the STR_TO_DATE function to convert the strings into DATE datatype, and then do the comparison. MySQL won't be able to make use of an index range scan operation, it will need to evaluate that function on every flipping row in the table.
As an example:
SELECT t.mycol
FROM mytable t
WHERE STR_TO_DATE(t.mycol,'%d %M %Y') >= '2014-01-01'
AND STR_TO_DATE(t.mycol,'%d %M %Y') < '2015-01-01'
We'll need to check the MySQL Reference Manual to verify that '%M' is the right format specifier for the full month name...
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Yes, it looks like I guessed right. M is the month name.
As I already commented, you should store your date as Timestamp or Data format then you could simply compare.
However, there is still a solution.. You can convert the varchar to a date directly in your query :
select * from yourTable
where (str_to_date(tb_date, '%d %M %Y') between '2014-01-01' and '2014-12-31');
But please don't use this hack and change your date format...
Edit : If you are really willing to use varchar to store your date, change it to varchar(17) which is the max character possible using your string format.

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.

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.

mysql query - format date on output?

In my table, dates are stored like this: 2011-03-03T13:30:00
I'm trying to output dates like this: March 3, 2011 1:30 PM
I'd much rather work it into the query rather than use php to format it, but I'm having some difficulty doing that. Trying various iterations of DATE_FORMAT, but it's not giving me what I want, maybe because of the way it's being stored?
You basically have two different operations you may need to perform when handling dates: date to string and vice versa. The functions you can use are DATE_FORMAT() and STR_TO_DATE(). Full reference can be found in the manual.
Usage example:
SELECT
DATE_FORMAT(CURRENT_TIMESTAMP, '%d/%m/%Y %H:%i:%s'),
STR_TO_DATE('31/12/2001 23:55:00', '%d/%m/%Y %H:%i:%s')
If your dates are not real dates but strings, you'll need to convert twice: from string to date and again from date to string:
SELECT
STR_TO_DATE('2011-03-03T13:30:00', '%Y-%m-%dT%H:%i:%s'),
DATE_FORMAT(STR_TO_DATE('2011-03-03T13:30:00', '%Y-%m-%dT%H:%i:%s'), '%M %e, %Y %l:%i %p')
Use DATE_FORMAT:
DATE_FORMAT(date, "%M %e, %Y %h:%i %p")
The MySQL date storage format is actually YYYY-MM-DD, but using the str_to_date() and date_format() functions you can accept and generate any date format required.
select DATE_FORMAT(DateTable.MyDate,'%d %b %y')
from DateTable
would return
04 Nov 08
You should really use a DATETIME field for such things (and clean the input on the way in) rather than having to sort this out at the point of output.
Irrespective, you can simply use the DATE_FORMAT function to re-format your field into the format you require, although this might produce some un-expected results on a VARCHAR or CHAR field. (If so, you'll have to use STR_TO_DATE or failing that some various string functions to extract the date bits you require.)