How do we convert TIME and DATETIME values to numeric form in MySQL?

王林
Release: 2023-09-02 18:13:02
forward
1159 people have browsed it

在 MySQL 中,我们如何将 TIME 和 DATETIME 值转换为数字形式?

Converting TIME(N) and DATETIME(N) values to numeric form can be done by adding 0 (0) to them. The following are the rules for this type of conversion −

Convert to integer

When N is 0, TIME(N) and DATETIME(N) values will be converted to integers.

For example, the values of CURTIME() and NOW() can be converted to integer values as follows −

mysql> SELECT CURTIME(), CURTIME()+0; +-----------+-------------------+ | CURTIME() | CURTIME()+0 | +-----------+-------------------+ | 19:42:54 | 194254 | +-----------+-------------------+ 1 row in set (0.04 sec) mysql> SELECT NOW(), NOW()+0; +-------------------------+----------------------------------+ | NOW() | NOW()+0 | +-------------------------+----------------------------------+ | 2017-10-27 19:43:43 | 20171027194343 | +-------------------------+----------------------------------+ 1 row in set (0.00 sec)
Copy after login

Converted to DECIMAL

When N is greater than 0, TIME( N) and DATETIME(N) values are converted to integers.

For example, the values of CURTIME() and NOW() can be converted to decimal values as follows -

mysql> SELECT CURTIME(), CURTIME(3)+0; +-----------+-------------------+ | CURTIME() | CURTIME()+0 | +-----------+-------------------+ | 19:47:40 | 194740.575 | +-----------+-------------------+ 1 row in set (0.04 sec) mysql> SELECT NOW(), NOW(3)+0; +-------------------------+----------------------------------+ | NOW() | NOW()+0 | +-------------------------+----------------------------------+ | 2017-10-27 19:48:45 | 20171027194845.589 | +-------------------------+----------------------------------+ 1 row in set (0.00 sec)
Copy after login

The above is the detailed content of How do we convert TIME and DATETIME values to numeric form in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!