Home  >  Article  >  Database  >  What will be the output if we try to extract a time value by providing only a date value to the MySQL EXTRACT() function?

What will be the output if we try to extract a time value by providing only a date value to the MySQL EXTRACT() function?

WBOY
WBOYforward
2023-09-13 12:53:021230browse

如果我们尝试仅向 MySQL EXTRACT() 函数提供日期值来提取时间值,会输出什么?

When we try to extract hour value from date, EXTRACT() function will give output 0 with warning as shown in the example given below -

mysql> Select EXTRACT(Hour from '2017-10-20');

+---------------------------------+
| EXTRACT(Hour from '2017-10-20') |
+---------------------------------+
| 0                               |
+---------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> Show Warnings;

+---------+------+----------------------------------------------+
| Level   | Code | Message                                      |
+---------+------+----------------------------------------------+
| Warning | 1292 | Truncated incorrect time value: '2017-10-20' |
+---------+------+----------------------------------------------+
1 row in set (0.00 sec)

Now when we try to extract minute value from date, EXTRACT() function will give century value in date as output with warning as shown in the example given below- p>

mysql> Select EXTRACT(Minute from '2017-10-20');
+-----------------------------------+
| EXTRACT(Minute from '2017-10-20') |
+-----------------------------------+
| 20                                |
+-----------------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> Show Warnings;
+---------+------+----------------------------------------------+
| Level   | Code | Message                                      |
+---------+------+----------------------------------------------+
| Warning | 1292 | Truncated incorrect time value: '2017-10-20' |
+---------+------+----------------------------------------------+
1 row in set (0.00 sec)

Now when we try to extract seconds value from date, EXTRACT() function will give year value as output with warning as shown in the example given below-

mysql> Select Extract(Second from '2017-10-20');

+-----------------------------------+
| Extract(Second from '2017-10-20') |
+-----------------------------------+
| 17                                |
+-----------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> Show Warnings;

+---------+------+----------------------------------------------+
| Level   | Code | Message                                      |
+---------+------+----------------------------------------------+
| Warning | 1292 | Truncated incorrect time value: '2017-10-20' |
+---------+------+----------------------------------------------+
1 row in set (0.00 sec)

Trying to get the time value (hours, minutes and seconds) from the current date, i.e. by using Curdate() at the date position, we will get similar results.

The above is the detailed content of What will be the output if we try to extract a time value by providing only a date value to the MySQL EXTRACT() function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete