datetime/timestamp problem with replication from 5.6 to 5.7? - mysql

I'm replicating from mysql 5.6.33 to 5.7.41. I have a table with a datetime field. If I understand correctly, between 5.6 and 5.7 the decreased the space a datetime field uses because it doesn't store timezone data. (but a timestamp does).
This query works on 5.7 (note the presence of the timezone field):
select count(*) from login_activities where date_created < '2023-01-15 04:00:15 -0800';
This delete statement does not work:
delete from login_activities where date_created < '2023-01-15 04:00:15 -0800'
ERROR:
Error 'Incorrect datetime value: '2023-01-15 04:00:15 -0800' for column 'date_created' at row 1' on query. Default database: 'sms'. Query: 'delete from login_activities where date_created < '2023-01-15 04:00:15 -0800''
How can I get the delete to work in the same way the select works? I've even removed sql_mode entries but still can't get it to work in 5.7
edit:
not sure if this matters, but the error with the delete statement is happining during replication (5.6 -> 5.7), but I'm running the select statement manually. I haven't tried running the delete statement manually because it will through off the replication.

Related

MySQL 8.0.16: Select Date LIKE query fails

I have 3 MySQL/MariaDB servers running for application development and testing:
MySQL Community Server 5.7.32
MySQL Community Server 8.0.16
MariaDB Community Server 10.4.12
The servers are configured to run on different ports on the same machine.
SELECT ##sql_mode for all servers:
MySQL Community Server 5.7.32: NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
MySQL Community Server 8.0.16: STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
MariaDB Community Server 10.4.12: NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION
I stumbled across some SELECT queries in old code I maintain whose fails to run, for e.g. searching a date with a given pattern. The queries look like this:
SELECT * FROM `userTable` WHERE `birthDate` LIKE '2020-%-%';
SELECT * FROM `userTable` WHERE `birthDate` LIKE '%12%';
The table looks like this:
CREATE TABLE IF NOT EXISTS `userTable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` TEXT COLLATE utf8_unicode_ci,
`birthDate` DATE,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
When I run the queries on MySQL Community Server 8.0.16 they fail with the error Error Code: 1525. Incorrect DATE value: '2020-%-%'. and Error Code: 1525. Incorrect DATE value: '%12%'..
MySQL Community Server 5.7.32 and MariaDB Community Server 10.4.12 executes the queries successfully.
Why does MySQL Community Server 8.0.16 fails to execute these SELECT queries?
PS: The first SELECT query can be written as
SELECT * FROM `userTable` WHERE YEAR(`birthDate`) = 2020;
to run on all 3 servers.
EDIT: It's a bug in MySQL Community Server 8.0.16 and fixed in version 8.0.22.
You should be comparing a bona fide date column against valid date literals, not strings, e.g. to find records in 2020:
SELECT *
FROM userTable
WHERE birthDate >= '2020-01-01' AND birthDate < '2021-01-01'; -- sargable :-)
Or maybe:
SELECT *
FROM userTable
WHERE YEAR(birthDate) = 2020; -- not sargable though :-(

Oracle to MySQL timestamp conversion in MySQL insert statement

I've already exported data from an Oracle 11g instance as insert statements. They need to go into a MySQL 5.5 instance. Having trouble with the TIMESTAMP conversion; I know I can use the TO_TIMESTAMP function inline with INSERT statements in MySQL; however, am unsure as to the correct flags to use. As follows below, in linear order, is: the Oracle timestamp statement and my attempt at a MySQL compatible statement. Obviously it's not working (in that MySQL states there's a syntax error).
Oracle:
TO_TIMESTAMP('12/22/2015 5:08:59.245837 PM','fmMMfm/fmDDfm/YYYY fmHH12fm:MI:SS.FF AM')
MySQL:
TO_TIMESTAMP('12/22/2015 5:08:59.245837 PM','%m/%d/%Y %h:%i:%s')
What am I missing beyond flags for microseconds and AM/PM?
FOR MYSQL:
STR_TO_DATE('2/6/2015 5:20:43.000000 AM','%c/%d/%Y %l:%i:%s.%f %p')
assumed you export from SQL Developer.
before you export the whole data, change your datetime select query with :
TO_CHAR(COLUMN_NAME, 'YYYY-MM-DD HH24:MI:SS') as XXX
it will produce datetime with MySQL format. so when you get the insert query, you can run directly at MySQL
if you want the date and the timestamp of oracle to be compatible with MySql just alter you session
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH:MI:SS.FF'
it is better to alter session

SQL update statement - Postgres and mysql compatibility

In mysql, one can execute the following statement:
UPDATE trx SET lock_id=somevalue WHERE lock_id is NULL order by stamp desc limit 1
I'm looking for a statement or statements run in transaction that would accomplish the same thing and would work in both MySQL 5.7 and PostgreSQL 9.4
It is important for this to be atomic, as this statement is in MySQL.

MySQL 5.7: Problems with DATETIME = '0000-00-00 00:00:00'

since I upgraded my MySQL-Database to 5.7, I have problems when I try to set a datetime column to 0000-00-00 00:00:00:
UPDATE users
SET updateTime = '0000-00-00 00:00:00'
WHERE id = 123;
// #1292 - Incorrect datetime value: '0000-00-00 00:00:00' for column 'updateTime ' at row 1
If I run the Query with UPDATE IGNORE [...], it works fine.
The SQL-Mode NO_ZERO_DATE is not set. Is there any setting to fix this?
I realy would hate to add IGNORE to every query in the application.
I had the same problem when updating form Ubuntu 14.04 repo mysql 5.5.6? to mysql 5.7.9
I am using a perl script to do several load data infile where the date is sometimes 0000-00-00 00:00:00. I also get err 1292 in DBO tracewhich was solved by removing STRICT_TRANS_TABLES from sql mode in optionsfile.

Replace sys_extract_utc in MySQL

I'm using the following query in Oracle, I need the same in MySQL also.
select sys_extract_utc(systimestamp) into currGMTDate from dual;
I tried with CURRENT_TIMESTAMP instead of systimestamp
but the output that I got was a different one. How can I get the same result in MySQL for the above Oracle query?
I tried with the below query and it worked for me
select date_format(CONVERT_TZ( current_timestamp, ##session.time_zone, '+00:00' ),'%d-%m-%Y %r')