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