how to convert varchar to datetime format in mysql - mysql

I am trying to convert varchar to date time
This is what I have now, and it is not working for me. I always get the have value
STR_TO_DATE(REPLACE(LEFT('5/16/2011 20:14 PM', LOCATE('M' , '5/16/2011 20:14 PM')-3), '/',','),'%m-%d-%Y %T')
This following code returns 5,16,2011 20:14
select REPLACE(LEFT('5/16/2011 20:14 PM', LOCATE('M' , '5/16/2011 20:14 PM')-3), '/',',')
my current output is emply string now. it should be 2011-05-16 20:14:00
How can i get this to work?
thanks

If your varchar is like this:
5/16/2011 20:14 PM
you can convert it to datetime using this:
SELECT STR_TO_DATE('5/16/2011 20:14 PM', '%c/%e/%Y %H:%i')
or this to format it like you want:
SELECT DATE_FORMAT(STR_TO_DATE('5/16/2011 20:14 PM', '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')

Related

Change DateTime string into Date

I have a DateTime column, which (because of some reasons) has changed my DateTime values from DateTime to Strings. Now I want to change them back to Date only. How to do this?
e.g my date column has values like: "Sep 2, 2019 8:00:00 PM" (in string format)
select str_to_date('Date','%d-%m-%Y') as Date
from table
This code gives blank cells as a result
Your date's format is: '%b %e, %Y %h:%i:%s %p'.
Read more about the parameters you can use in str_to_date() here.
So do the conversion like this:
select str_to_date(
'Sep 2, 2019 8:00:00 PM',
'%b %e, %Y %h:%i:%s %p'
) as Date
See the demo.
Have you tried using the CONVERT function?
SELECT CONVERT(table.Date, DATE) AS Date FROM table;

Mysql str_to_date () return NULL

I have a string '10/01/2016 00:00' that I want convert to DATETIME
I tried like this:
select STR_TO_DATE('10/01/2016 00:00', '%d/%m/%Y %h:%i');
But it's not working. what i am doing wrong?
The problem is that %h expects an hour in the format 01-12 and you are providing an hour that is 00. You can use %H that expects an hour in the format 00-23, try with this:
select STR_TO_DATE('10/01/2016 00:00', '%d/%m/%Y %H:%i');

Converting Varchar (1/24/15 1:30:00 PM) to date format (2015-01-24 13:30:00)

I have this date format : 1/24/15 1:30:00 PM(Varchar), but I want to convert it changing into datetime format (2015-02-24 13:30:00) using query, how can I do it?
SELECT STR_TO_DATE('1/24/15 1:30:00 PM', '%m/%d/%y %h:%i:%s %p')
FROM yourTable
If you don't like the format of the output this gives you, you can force it to what you want by wrapping STR_TO_DATE() in a call to DATE_FORMAT():
SELECT DATE_FORMAT(STR_TO_DATE('1/24/15 1:30:00 PM', '%m/%d/%y %h:%i:%s %p'),
'%Y-%m-%d %h:%i:%s')
FROM yourTable

Mysql Select statement to and convert timestamp on the fly

I have a MYSQL table which has a column name called timestamp stored in string format. the timestamps in this column are in the following format e.g. '20/10/2014 05:39 PM'
Now how can select a row and convert the timestamp column to 24HR format on the fly.
basically I want something like this.
SELECT id, user, STR_TO_DATE(timestamp, '%d/%m/%Y %h:%i %p') as timestamp FROM mytable WHERE user="bob";
but this does not work. looks like its not recognizing the timestamp variable inside STR_TO_DATE sql function and its retuning NULL for the timestamp column.
Please help.
Looks fine to me:
mysql> SELECT STR_TO_DATE('20/10/2014 05:39 PM', '%d/%m/%Y %h:%i %p');
+---------------------------------------------------------+
| STR_TO_DATE('20/10/2014 05:39 PM', '%d/%m/%Y %h:%i %p') |
+---------------------------------------------------------+
| 2014-10-20 17:39:00 |
+---------------------------------------------------------+
(I know this should be a Comment, but is an Answer in order to get formatting.)
use date_format function instead of str_to_date:
SELECT id, user, date_format(timestamp, '%d/%m/%Y %h:%i %p') as timestamp FROM mytable WHERE user="bob";

Convert datetime in MySQL

How can we convert the time in AM/PM to 24-hrs format in MySQL? Example 2:50 PM should be converted to 14:50. Please help me to convert this.
use %p with STR_TO_DATE()
SELECT STR_TO_DATE( '2:50 pm', '%h:%i %p' ) // will return 14:50:00