I have a table which pulls data from RSS Feeds and saves. The RSS feed format of representing publication date and time seems difficult to be converted into a MYSQL DATE(TIME) format. It comes as Thu, 14 Jan 2021 17:11:05 +0000 for example. So when I use STR_TO_DATE to try converting it, it returns null since STR_TO_DATE doesn't go with such formats. Also, I can't even trim parts of the string and make it work with STR_TO_DATE. Of course, other functions like UNIX_TIMESTAMP won't work with that kind of string. How can I convert such a complex string to a MYSQL date format? I'm trying to do something like:
SELECT * FROM tbl WHERE 1 ORDER BY STR_TO_DATE('Thu, 14 Jan 2021 17:11:05 +0000', '%d-%m-%Y')
You need to complete your date format.
You can truncate the parts you don't want afterwards.
As an alternative you could use substring before convertion.
select str_to_date(
'Thu, 14 Jan 2022 17:11:05 +0000',
'%a, %d %b %Y %H:%i:%s +%f') formatted_date
| formatted_date |
| :------------------------- |
| 2022-01-14 17:11:05.000000 |
db<>fiddle here
Related
I use the below method to convert Unix Time stored in the hltime column of a MySQL DB to a readable format - "YYYY DD MM hh:mm:ss"
For Example:
UTC date and time stored format in MySQL DB: 1500473820
MySQL Function used to convert to readable format:
from_unixtime(hltime,'%Y %D %M %h:%i:%s')
Result: "2017 19th July 07:47:00"
The date and time stored in the hltime column are in GMT. Thus how could I use the from_unixtime() function (or any other function) to convert the values shown into IST (Indian Standard Time).
I have tried using convert_tz(from_unixtime(hltime,'%Y-%D-%M %h:%i:%s'),'+00:00','+06:00'), but I get NULL values.
Date time format used to convert is not correct
Change:
'%Y %D %M %h:%i:%s'
To:
'%Y-%m-%d %h:%i:%s'
And it should be working
Example:
SELECT #ut:= 1500473820 AS ut
, #ts:=FROM_UNIXTIME(#ut,'%Y-%m-%d %h:%i:%s') AS gmt
, CONVERT_TZ(CAST( #ts AS DATETIME ),'+00:00','+05:30') AS ist;
Result:
---------- ------------------- ---------------------
ut gmt ist
---------- ------------------- ---------------------
1500473820 2017-07-19 07:47:00 2017-07-19 13:17:00
I have a legacy table which has a varchar column represent date, format is MM/DD/YYYY (e.g. 01/08/2015). It is not convenient to perform data range selection since it is a varchar (when I use < or > kinds comparison, it goes to varchar/string comparison, which have different results from date comparision).
For example, I want to select only rows which dates are between 01/08/2015 and 01/10/2015. Any smart solution is appreciated, and I cannot change the data type of varchar to date in my existing table.
I am using MySQL Workbench/MySQL.
Varchar dates are evil and they are not real date, the best solution is to use mysql's native date data types.
Since you can't change the datatype you may use str_to_date() function and here how it works
mysql> select str_to_date('01/08/2015','%d/%m/%Y') as d ;
+------------+
| d |
+------------+
| 2015-08-01 |
+------------+
So the query for select would be
select * from table_name
where
str_to_date(date_column,'%d/%m/%Y')
between
str_to_date('01/08/2015','%d/%m/%Y')
and
str_to_date('01/10/2015','%d/%m/%Y')
There are many answers which addresses many different way of converting the string to date.
You may choose whichever is perfect for your need
SELECT * FROM `your_table` WHERE DATE_FORMAT(my_column_with_the_string_date, "%Y-%m-%d") <= '2011-09-30'
DATE_FORMAT can be used to convert your date string to any format: I will use the NOW() function instead of string to list different
formats that are supported
DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
DATE_FORMAT(NOW(),'%m-%d-%Y')
DATE_FORMAT(NOW(),'%d %b %y')
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')
The output of the above is:
Nov 04 2014 11:45 PM
11-04-2014
04 Nov 14
04 Nov 2014 11:45:34:243
You can modify your query accordingly
You can cast your dates as strings using STR_TO_DATE:
STR_TO_DATE(yourdatefield, '%m/%d/%Y')
SELECT STR_TO_DATE(got_fired_at, '%m/%d/%Y') BETWEEN ? AND ? FROM firings;
(field/table names guaranteed to have been chosen randomly)
Use MySQL's STR_TO_DATE function to parse the date strings to date objects then do the comparison.
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.
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.
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