I my MySQL (version 5.7.25) database there's have a column, which is full of varchar(100) dates like Fri May 04 08:08:42 UTC 2018. I need to convert them to date.
So far I've came up with
SELECT STR_TO_DATE('Fri May 04 08:08:42 UTC 2018', '%a %b %d %T %Y') AS to_date
but it returns null because of the timezone. But if I try:
SELECT STR_TO_DATE('Fri May 04 08:08:42 2018', '%a %b %d %T %Y') AS to_date
... it works perfectly. So is there a way to add a timezone to the date format?
If the string always contains UTC then you can hard-code it:
SELECT STR_TO_DATE('Fri May 04 08:08:42 UTC 2018', '%a %b %d %T UTC %Y') AS to_date
-- 2018-05-04 08:08:42
If you want to convert the timezone information as well you need to use CONVERT_TZ function:
SELECT CONVERT_TZ(STR_TO_DATE('Fri May 04 08:08:42 UTC 2018', '%a %b %d %T UTC %Y'), '+00:00', 'system')
-- 2018-05-04 13:08:42
-- actual result depends on system timezone
Related
I have a data "Fri Jan 10 00:00:00 UTC 1992" And I want to convert it to 1992-10-01.
I know how to do it with Javascript, but I have no idea how to do it with MySQL
I tried :
DATE_FORMAT(column, '%d/%m/%Y')
But it's return null
select date_format(str_to_date("Fri Jan 10 00:00:00 UTC 1992","%a %b %d %H:%i:%s UTC %Y"),"%Y-%m-%d");
First you need str_to_date, using "%a %b %d %H:%i:%s UTC %Y" to get a date, after that you can format the date using date_format in the desired format.
These specifiers shown in the following table may be used in the format string.
I have stored date in string format like 'Thu, 24 Dec 2020 07:54:35 GMT'.
Can someone please suggest how to convert this string into YYYY-MM-DD format using MySQL query.
Tried this function :
str_to_date(dateTime, '%a %d %b %Y %T %x')
DATE_FORMAT(dateTime, '%Y-%m-%d)
DATE(dateTime)
You can just ignore stuff that comes at the end (assuming you're happy to store the time for the given time zone)...
E.g.
select str_to_date('Thu, 24 Dec 2020 07:54:35 GMT', '%a, %e %b %Y %T') dt;
I have a column in my MySQL database which has timestamp data in string format. I want to convert it into timestamp format.
Below is the sample data:
Date
--------------------------------
Fri Dec 14 14:11:43 IST 2018
Fri Dec 14 14:13:20 IST 2018
I'm expecting the result to be in the following format:
14-12-2018 14:11:43
14-12-2018 14:13:20
By using the STR_TO_DATE() to convert the string into a DATETIME, then by using DATE_FORMAT() can change it to the expected date time format.
The following query will return the expected output date time format:
SELECT DATE_FORMAT(
STR_TO_DATE('Fri Dec 14 14:11:43 IST 2018', '%a %b %d %T IST %Y'),
'%d-%m-%Y %H:%i:%s');
Output:
14-12-2018 14:11:43
db<>fiddle demo
Reference formats are available in this link
You could use str_to_date()
str_to_date('Fri Dec 14 14:11:43 IST 2018', '%a %b %d %T IST %Y');
In a table say testTable we are storing date-time as varchar in following format
Tue May 09 15:16:54 IST 2017
I am trying to write a query which gives me all records between two dates using STR_TO_DATE to convert the date in varchar format to datetime. However below query is failing with Error Code: 1241 Operand should contain 1 column(s)
SELECT * FROM test12.testTable a WHERE a.timestamp BETWEEN (STR_TO_DATE('Tue May 09 17:26:11 IST 2017', '%a %b %d %H:%i:%s %Z %Y'),
STR_TO_DATE('Wed May 10 20:17:11 IST 2017', '%a %b %d %H:%i:%s %Z %Y'));
Could you suggest what is wrong here?
When your dates are stored in this weird way, you want to use the STR_TO_DATE() function on the column, not on the string you provide.
SELECT * FROM test12.testTable a
WHERE STR_TO_DATE(a.timestamp, '%a %b %d %H:%i:%s %Z %Y')
BETWEEN '2017-05-09 17:26:11' AND '2017-05-10 20:17:11';
Problem
I am trying to fetch all the records from table where date_time field is greater than 'Thu, 11 Jul 2013' by running the below mentioned query. Value in the date_time field is stored in this format => Thu, 11 Jul 2013 08:29:37. Any help will be great.
Datatype of field date_time is varchar
Query
SELECT * FROM table_name
WHERE username = 'mark#example.com'
AND STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= 'Thu, 11 Jul 2013 00:00:00';
Here is yet another great example of why you should implement date/time fields in MySQL using the date, datetime, or timestamp field types and let your application deal with how to format the date for output.
Right now you are going to need to do something like:
SELECT * FROM table_name
WHERE username = 'mark#example.com'
AND STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= STR_TO_DATE('Thu, 11 Jul 2013 00:00:00', '%a, %e %b %Y %H:%i:%s');
This query will not be able to use any index you have on your date_time field, so the query will be very inefficient. It will need to perform a full table scan, converting the value of each row in order to make the comparison.
What you should be doing is:
SELECT * FROM table_name
WHERE username = 'mark#example.com'
AND date_time >= STR_TO_DATE('Thu, 11 Jul 2013 00:00:00', '%a, %e %b %Y %H:%i:%s');
Here if you have your field in the MySQL datetime format, you just need to convert the input to a this format for matching. Since your field data is already in this format, you will be able to utilize an index for the search.
You are trying to compare a date with a String.
The str_to_date function application is correct, but you are not comparing to a date.
The right way to do it is:
select * from yourTable
where STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= '2013-07-11 00:00:00'
Notice that the date format is YYYY-MM-DD HH:mm:ss (which is MySQL default date format).
Of course, you can also compare to str_to_date results:
... where STR_TO_DATE(date_time, '%a, %e %b %Y %H:%i:%s') >= STR_TO_DATE('Thu, 11 Jul 2013 00:00:00', '%a, %e %b %Y %H:%i:%s')