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');
Related
how to compare time with current time but with AM pm format also compare in mysql select query.
I added Database image where stored time in this formate and i compare to current time but i didn't get success.
You need to convert expectedDeliveryDateTime to a date so you can compare it with NOW(). To do that you need to use STR_TO_DATE with the format '%d/%m/%Y %h:%i %p' which matches the data in your image. So try:
SELECT *
FROM yourtable
WHERE STR_TO_DATE(expectedDeliveryDateTime, '%d/%m/%Y %h:%i %p') >= NOW()
I have assumed you are looking for dates in the future, if you want dates in the past just change >= NOW() to <= NOW().
use now()and format date using date_formatas your column is varchar so you have to convert it date by using STR_TO_DATE function
select * from t where
STR_TO_DATE(expectedDeliveryDateTime, '%d/%m/%Y %h:%i %p') >=date_format(now(), '%d/%m/%Y %h:%i %p')
Please, check my time zone. Are we the same?
SELECT UTC_TIMESTAMP() from dual;
If the same, then you compare is correct.
I have this query
SELECT * FROM tracklogs.sms_outbound
WHERE gsmno = 'rk4#*******.com.ph'
AND cdate > cast('2013/11/14 09:44:48 PM' as datetime)
where cdate format is in %Y-%m-%d %h:%i:%s %p.
I have tried converting the date into that format then cast it as datetime but still doesn't working.
Use STR_TO_DATE() to correctly convert the datetime literal you have provided to a proper DATETIME value. It seems that your cdate column is a char() or varchar() column. So you will also need to convert that to DATETIME to compare it.
What you need is this:
That works like this (http://sqlfiddle.com/#!2/d41d8/48741/0)
STR_TO_DATE(cdate, '%Y-%m-%d %h:%i:%s %p') >
STR_TO_DATE('2013/11/14 09:44:48 PM', '%Y/%m/%d %h:%i:%s %p')
Converting these strings to DATETIME data items ensures that the comparison handles both the date and the time correctly. See this fiddle (http://sqlfiddle.com/#!2/d41d8/48743/0)
But, you should consider changing your cdate item to a DATETIME, because then you'll be able to index it and speed up your search.
SELECT * FROM tracklogs.sms_outbound
WHERE gsmno = 'rk4#*******.com.ph'
AND UNIX_TIMESTAMP(str_to_date(cdate,'%Y-%m-%d %H:%i:%s')) > UNIX_TIMESTAMP('2013-11-14 09:44:48')
I am new to mysql. I need to insert the string '2014-07-10 13:33:33' into the table column which has datetime datatype.
I gave like this,
SELECT STR_TO_DATE('2014-07-09 23:30:00', '%Y/%m/%d %h:%m:%s');
But i didn't not give the result.
How to do this?
Minutes is %i, not %m and 24-hour format is %H, not %h:
SELECT STR_TO_DATE('2014-07-09 23:30:00', '%Y-%m-%d %H:%i:%s');
Shouldnt it be
SELECT STR_TO_DATE('2014-07-09 23:30:00', '%Y-%m-%d %H:%i:%s');
%Y for year numeric, four digits
%m for month numeric
%d for Day of the month, numeric
%H for 24 hours
%i for minutes
%s for seconds
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')
What's wrong with this? str_to_date('26/04/2011 00:00:00', '%d/%m/%Y')
It gives Error Code: 1292 Truncated incorrect date value: '26/04/2011 00:00:00'
Update: The problem is the 00:00:00, if I remove it it works. How can edit the '%d/%m/%Y' to accept the time? '%d/%m/%Y %h:%m:%s' doesn't work.
str_to_date('26/04/2011 00:00:00', '%d/%m/%Y %H:%i:%s')
Note the capital %H for hour (00-24) instead of %h (01-12).
Since you've specified the time in the value parameter, you should also specify the time components in the date format parameter.
str_to_date('26/04/2011 00:00:00', '%d/%m/%Y %h:%i:%s')
either that, or drop the time component from your date value:
str_to_date('26/04/2011', '%d/%m/%Y')
either should work, but you need to be consistent between the two parameters.
Alternatively, you could specify the format so that it has fixed values in the time component:
str_to_date('26/04/2011 00:00:00', '%d/%m/%Y 00:00:00')
this will work, but only if the time component is always 00:00:00.