MySql datetime format change - mysql

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;

Related

convert full date string to date mysql

I have database which contains string like this
22 Jan 2019 11:03
I would like to convert this string to date so I apply this query
select DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d-%m-%Y') ,'%d-%m-%Y');
but I get a null result
All you have to do is change small letter m to big letter M in your str_to_date function.
select STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y');
so the final query would be:
select DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y') ,'%d-%m-%Y');
Here is a demo
SELECT STR_TO_DATE('22 Jan 2019 11:03','%d,%m,%Y');
Basically you first need to understand the values to pass in STR_TO_DATE() and DATE_FORMAT() functions
STR_TO_DATE(my date string , current format of my date string)
Now what you are not doing right is that in STR_TO_DATE() you are passing the format '%d-%m-%Y' , this format says that the input string has hyphen separated date month and year values which is not true.
Now in your case the actual format of your date string is the following
'%d %M %Y %h:%i'
STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y %h:%i')
%d - day
%M - month
%Y - year
%h - hour
%i - minute
For more info on formats click
Now that we have a complete valid value from string to date including hour and minutes, we can convert this date into any of our required format using correct parameters
SELECT DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y %h:%i') ,'%d-%m-%y %h:%i');
You can try out various examples here
https://www.mysqltutorial.org/tryit/query/mysql-str_to_date/#1

STR_TO_DATE with timezone

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

Converting string to timestamp format in MySQL

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

Parsing the date with timezone MySQL

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

Select records where datetime is greater than the specified date

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