Rounding operation of datetime field in MySQL

Rounding operation of datetime field in MySQL

Preface

If I hadn't fallen into the trap, I probably wouldn't have known that the time field would be rounded up.

1. Background

The maximum time of the day is obtained through Java code and then stored in the database. The database table field format datetime retains 0 bits.

now.with(LocalTime.MAX)

A small line of code to get the maximum date of today.

I checked the database and found that the next day's time was actually stored there.

It looks like it’s rounded off!

2. Simulation test

After execution, take a look at the log:

It uses 2021-09-28T23:59:59.999999999, but strangely the database stores 2021-09-29 00:00:00.

Try using SQL directly:

This... is indeed rounded off.

Try MariaDB instead!

docker pull mariadb

docker run -d --name mariadb -p 33306:33306 -e "MYSQL_ROOT_PASSWORD=root" mariadb

docker exec -it mariadb bash

MariaDB discards extra digits directly!

3. Conclusion

If the value passed into MySQL time exceeds the precision range, it will be rounded.

If the value passed into MariaDB time exceeds the precision range, it will be discarded directly.

I stepped on a small pit. Finally, hard code it!

now.with(LocalTime.parse("23:59:59"))

Summarize

This is the end of this article about rounding of datetime fields in MySQL. For more information about MySQL datetime rounding, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • SQL Practice Exercise: Online Mall Database User Information Data Operation
  • MySQL learning database operation DML detailed explanation for beginners
  • MySQL learning to create and operate databases and table DDL for beginners
  • Detailed explanation of the mysql database LIKE operator in python
  • SQL Practice Exercise: Online Mall Database Product Category Data Operation

<<:  JavaScript String Object Methods

>>:  HTML design pattern daily study notes

Recommend

Detailed process of deploying Docker to WSL2 in IDEA

The local environment is Windows 10 + WSL2 (Ubunt...

How to mark the source and origin of CSS3 citations

I am almost going moldy staying at home due to th...

Pricing table implemented with CSS3

Result: Implementation Code html <div id="...

Execution context and execution stack example explanation in JavaScript

JavaScript - Principles Series In daily developme...

The presentation and opening method of hyperlink a

<br />Related articles: How to prompt and op...

HTML+CSS+JavaScript to create a simple tic-tac-toe game

Table of contents Implementing HTML Add CSS Imple...

css3 animation ball rolling js control animation pause

CSS3 can create animations, which can replace man...

Detailed steps for deploying Microsoft Sql Server with Docker

Table of contents 1 Background 2 Create a contain...

Design a data collector with vue

Table of contents Scenario Core Issues Status mon...