MySQL TIMESTAMP stoped working on summer time transition - mysql

I have a stored procedure that was crashing yesterday. It begins like this:
DECLARE v_today TIMESTAMP;
SET v_today = (DATE(NOW()) + INTERVAL 0 SECOND);
I've fixed it changing the type of v_today to DATETIME. But I want to understand why it was generating an error only yesterday (when Brazil started summer time).
The error was:
Mysql2::Error: Incorrect datetime value: '2017-10-15 00:00:00' for column 'v_today' at row 1:
Thanks.

I bet this is a bug in MySQL. Here's the time transition discipline for Brazil. timeanddate.com/time/change/brazil/brasilia It looks like the TIMESTAMP value 2017-10-15 00:00:00 doesn't exist, and needs to be pushed forward an hour to 2017-10-15 01:00:00. But MySQL just gacks.

Related

What's wrong with '2018-03-22 00:00:00" in MySQL?

I want to update a date field and set it to 2018-03-22 00:00:00 but I get the following stupid error:
Error Code: 1292. Incorrect datetime value: '2018-03-22 00:00:00' for column 'Date' at row 158917
This is the query I use for updating:
update assets.transactions
set date = date_add(date, interval 1 hour)
where date between '2018-03-21 23:00:00' and '2018-06-29 23:59:59';
What is wrong? I searched a lot and found out dates before 1970-01-01 00:00:01 are not supported by MySQL, that is acceptable, but a date in the middle of 2018? That's something I can't digest.
Is there any solution to make this work right?
I guess you're updating a TIMESTAMP column. I also guess you have your MySQL instance set to a timezone with a daylight time switchover on 23-March-2018. I guess the rules of your timezone switchover in your country mean that the clock rolls over from 21-March-2018 11:59:59 to 22-March-2018 01:00:00.
So the value 2018-03-22 00:00:00 just doesn't exist.
Strange, isn't it?
Try issuing this MySQL command, to set the time zone to UTC, before doing these sorts of mass timestamp updates.
SET time_zone = 'UTC';
Don't forget to switch it back before doing other operations. Or just do those operations from a different MySQL connection.

Get unix_timestamp for date in mysql

I have a column with type datetime with values that look like
id updated_at
1, 2015-03-04 18:49:41
2, 2016-05-24 19:28:43
3, 2014-07-29 19:15:44
The date stored in the database is in UTC timezone.
Now when i retrieve it, I want to retrieve it as UTC time, and convert that time to a unit_timestamp.
However, whenever i do
SELECT unix_timestamp(updated_at) from table
I think it auto added my local timezone when parsing the date and gave me a unix_timestamp that is ~ 4 hours (I am in Eastern time) off from the timestamp that i was expecting.
How should i approach this issue? I can obviously just remove the 4 hours offset, but things get tricky when some dates had a 5 hours offset due to daytimesaving stuff.
I got it working by
UNIX_TIMESTAMP(CONVERT_TZ("some_date", '+00:00', 'SYSTEM'))

Mysql Convert_tz

I'm trying to convert a datetime from Asia/Manila to EST timezone
without declaring the exact interval like
date_sub(), subdate(), date_add(), adddate()
i find it easy to use
SELECT DATE_SUB('2016-04-04 13:00:00', INTERVAL 12 HOUR);
the result will be2016-04-04 01:00:00
But Im trying to create a dynamic script where i don't need to look how many hours is the difference between two timezone
and i find Convert_TZ() to do job
SELECT CONVERT_TZ('2016-04-04 13:00:00', 'Asia/Manila', 'EST');
but the result of this query is 2016-04-04 00:00:00
Maybe this native function is not including the "Daylight saving time(DST)"
Does anyone know how to do the trick?
where i can easily convert the time including the DST
to any timezone without hard coding the interval hour between the two timezone?
Thanks
Okay, my problem is solved, i use two option
First :
I simply use 'US/Eastern' not 'EST' to include the daylight in conversion.
Second:
Because I didn't know the first option earlier i do this to solve my problem at first.
I create a table that compose of the date where it is DST
which i found in some site online..
Then
I create a mysql function where its lookup to the table above
which if the specified date is between that DST Start and DST End it will automatically add 1 hour,
My function is like this,
CREATE FUNCTION usp_Convert(specified_date DATETIME, From_Timezone VARCHAR(20), To_Timezone VARCHAR(20), is_DST INT(1)) RETURNS datetime
DECLARE theDate DATETIME;
SET theDate = CONVERT_TZ(specified_date, From_Timezone, To_Timezone);
IF is_DST = 1 AND To_Timezone= 'EST' THEN
SET theDate = ADDDATE(theDate, INTERVAL 1 HOUR); END IF;
RETURN theDate;
This might not be the best answer but this totally solved my problem
Thanks.

Mysql delete from table where date is 'yesterday' but ignoring the time stamp

I have a table in Mysql which has column called 'dep_timestamp' which holds data in the following format (the data is received from a external source so can't be changed, and is displayed via web queries so can't be modified within that table)
2015-05-12 19:18:00 +0100
The database holds cancellations for booked taxi journeys which get pushed out to me from a central booking system in realtime. Throughout the day I will get any number of messages for cancelled journeys. A journey has a booked departure time dep_timestamp in its full format of 2015-05-12 19:18:00 +0100 that is used for reporting and all sort of other things.
Every day at 03:00 I want to delete all of the cancelled journeys that where due to depart 'yesterday' This means when my users do a query and ask what journeys have been cancelled today they only see stuff that has a booked departure of today.
I have an event setup on the server to delete rows older then 1 day using the following code;
DELETE FROM db.canx_today WHERE 'dep_timestamp' < DATE_SUB(CURRENT_TIME() , INTERVAL 1 DAY)
That event is set to run every day at 03:00 and does without error. However it takes the full date/time into consideration when running which means it only deletes the rows where the time & date are both older than one day.
If I swap CURRENT_TIME with CURRENT_DATE then the server throws this error; Truncated incorrect datetime value: '2015-05-13 10:17:00 +0100' which makes sense in so far that its looking for a full date/time string.
Is there a way to ignore the time element and just delete all rows that are from the previous day?
You can calculate based on CURRENT_DATE() and just concatenate 00:00:00 to that value.
WHERE `dep_timestamp` < CONCAT(CURRENT_DATE(), ' 00:00:00')
This should work, but will only be noticeably faster than the one I originally put in the comments above if dep_timestamp is indexed.
WHERE `dep_timestamp` < DATE_FORMAT(curdate(), "%Y-%m-%d 00:00:00")
Since DATE_FORMAT() actually returns a string, this might be more efficient when indexes are actually needed:
WHERE `dep_timestamp` < CAST(DATE_FORMAT(curdate(), "%Y-%m-%d 00:00:00") AS DATETIME)
DELETE FROM `canx_today`
WHERE DATE(`dep_timestamp`) = DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY);

updating day in mysql

Accidentally the server date was set to a wrong value for 1 day. You have correctly set the date now. But you want to change all the date entries made on that day. Write a query to change the day by 1 of all dates in the query table on 31st Jan 2013.
I have written the following query
UPDATE query SET date= DATE_ADD(date, INTERVAL 1 DAY)
WHERE date="2013-01-31";
but it is not executing correctly
Your code is almost correct. But you need to do following change.
UPDATE query SET date= DATE_ADD(date, INTERVAL 1 DAY) WHERE date(date)="2013-01-31";
I hope this works for you.