MySQL str_to_date problem - mysql

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.

Related

How to set year to 2020 if there is no year set on a VARCHAR date

I'm working with a quite old database... And dates are stored as VARCHAR, and I need to keep dates as VARCHAR :S but expanding it with year=2020, hour=0, minute=0, and second=0 if that fields are not set.
Dates have all kind of weird formats, such as '01/08', '01/08/20', '01/08/2020' and so on. I'm dealing good with all formats, except when year is not set, I have tried with this query, but it set the year to 0 and causes an error with STR_TO_DATE() function
UPDATE people
SET date =
IF (
YEAR(STR_TO_DATE(date, '%d/%m/%Y %H:%i:%s')) > 0 ,
DATE_FORMAT(STR_TO_DATE(date, '%d/%m/%Y %H:%i:%s'), '%d/%m/%Y %H:%i:%s') ,
DATE_FORMAT(STR_TO_DATE(date, '%d/%m/%Y %H:%i:%s') + INTERVAL 2020 YEAR, '%d/%m/%Y %H:%i:%s')
)
Any idea dudes?

How to compare current time with 26/09/2018 04:10 AM

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.

mysql CONVERT_TZ inconsistent behavior

The following behavior puzzles me. My mysql server is installed on a PC with EEST timezone.
I want to convert a datetime in mysql to UTC date.
When executing
select CONVERT_TZ('1970-01-01 02:00:01', 'SYSTEM', '+00:00')
the reponse is:
'1970-01-01 00:00:01'
Similarly, when executing
select CONVERT_TZ('1970-01-01 04:00:00', 'SYSTEM', '+00:00')
the response is:'1970-01-01 02:00:00'
However, when I basically want the 0 unix time, by executing:
select CONVERT_TZ('1970-01-01 02:00:00', 'SYSTEM', '+00:00')
The reponse is again: '1970-01-01 02:00:00'
Why is that? Am I doing something wrong? How can I get the correct value which is '1970-01-01 00:00:00'?
I think the issue here is that an adjustment made in CONVERT_TZ which would result in a timestamp on or before the start of the UNIX epoch will be ignored, and the original input timestamp will be returned. Consider the following three queries:
SELECT CONVERT_TZ('1970-01-01 01:00:01', 'SYSTEM', '+00:00');
-- '1970-01-01 00:00:01'
SELECT CONVERT_TZ('1970-01-01 01:00:00', 'SYSTEM', '+00:00');
-- '1970-01-01 01:00:00'
SELECT CONVERT_TZ('1970-01-01 00:30:00', 'SYSTEM', '+00:00');
-- '1970-01-01 00:30:00'
Only the first query adjusts the timestamp; the second two just return the input.
From the excellent comments given by #Thomas above, I pulled the following from the MySQL documentation for CONVERT_TZ:
If the value falls out of the supported range of the TIMESTAMP type when converted from from_tz to UTC, no conversion occurs.

STR_TO_DATE from CONCAT

I have one date-field ref_event_times.end_date and one time-field ref_event_times.end_time in my table "ref_event_times"...
I try to union that as a one datetime field "end_date_time"...
Use follow
STR_TO_DATE('CONCAT(`ref_event_times`.`end_date`,' ',`ref_event_times`.`end_time`)','%m/%d/%Y %H:%i') AS `end_date_time`
return null...
Where is the mistake?
MYSQL doesn't support your given datetime format '%m/%d/%Y %H:%i'
The mysql date format should be like this '%Y-%m-%d %H:%i:%s' Datetime format in mysql
Data Type “Zero” Value
DATE '0000-00-00'
TIME '00:00:00'
DATETIME '0000-00-00 00:00:00'
TIMESTAMP '0000-00-00 00:00:00'
YEAR 0000
try this example
select STR_TO_DATE(CONCAT(current_date,' ',current_time),'%Y-%m-%d %H:%i:%s');

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