3

I have following code:

CREATE TABLE test_tab (
  create_time datetime(6) NOT NULL,
  PRIMARY KEY (create_time)
);

INSERT INTO test_tab (create_time) VALUES ('2023-04-21 00:17:24.000000000');
COMMIT;

SELECT * FROM test_tab WHERE create_time = '2023-04-21 00:17:24.000000000';
DELETE FROM test_tab WHERE create_time = '2023-04-21 00:17:24.000000000';
COMMIT;
SELECT * FROM test_tab WHERE create_time = '2023-04-21 00:17:24.000000000';

Both selects return one row. Any clues why it might happen?

$ mysql --version mysql Ver 15.1 Distrib 10.3.38-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

1

1 Answer 1

2

The problem is that you have more than 6 microseconds, so you need to convert it first

for the select, it automatically converts it, which the DELETE doesn't

CREATE TABLE test_tab (
  create_time datetime(6) NOT NULL,
  PRIMARY KEY (create_time)
);

INSERT INTO test_tab (create_time) VALUES ('2023-04-21 00:17:24.000000000');
COMMIT;

SELECT * FROM test_tab WHERE create_time = '2023-04-21 00:17:24.000000000';


create_time
2023-04-21 00:17:24.000000
DELETE FROM test_tab 
  WHERE create_time = STR_TO_DATE('2023-04-21 00:17:24.000000000','%Y-%m-%d %H:%i:%s.%f');


SELECT * FROM test_tab WHERE create_time = '2023-04-21 00:17:24.000000000';
create_time

fiddle

3
  • This is unsettling, as this behaviour is put on display due to an external software which I can barely modify. I hoped that a configuration change (or setting something on driver connection/connection session) could solve the problem.
    – enedil
    Apr 21 at 20:17
  • Even if I insert without the precision of 9 digits, the issue still remains.
    – enedil
    Apr 21 at 20:20
  • no you must use str_to_date, there is no other solution for mariadb, as it will only support 6 digits is tried to change the digits to a numbe rhiger than 6 and it will not work.
    – nbk
    Apr 21 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.