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

Linux unlink function and how to delete files

1. unlink function For hard links, unlink is used...

MySQL database optimization: index implementation principle and usage analysis

This article uses examples to illustrate the prin...

XHTML Getting Started Tutorial: Simple Web Page Creation

Create your first web page in one minute: Let'...

MySQL database must know sql statements (enhanced version)

This is an enhanced version. The questions and SQ...

9 Tips for Web Page Layout

<br />Related articles: 9 practical suggesti...

What are the image file formats and how to choose

1. Which three formats? They are: gif, jpg, and pn...

Use Xshell to connect to the Linux virtual machine on VMware (graphic steps)

Preface: I recently started to study the construc...

js to achieve floor scrolling effect

This article uses jQuery to implement the sliding...

Common array operations in JavaScript

Table of contents 1. concat() 2. join() 3. push()...

How to represent various MOUSE shapes

<a href="http://" style="cursor...

About Zabbix custom monitoring items and triggers

Table of contents 1. Monitoring port Relationship...

MySQL trigger detailed explanation and simple example

MySQL trigger simple example grammar CREATE TRIGG...

HTML+CSS div solution when relative width and absolute width conflict

Div solution when relative width and absolute wid...