TO_SECONDS()

Description

The TO_SECONDS(expr) function is used to calculate the number of seconds between a given date or datetime expr and the year 0, January 1, 00:00:00. If expr is NULL, it returns NULL.

Note

The dates 0000-00-00 and 0000-01-01 are considered invalid. MatrixOne year queries should start from 0001. When querying 0000-00-00 and 0000-01-01, TO_SECONDS() returns an error message:

  1. mysql> SELECT TO_SECONDS('0000-00-00');
  2. ERROR 20301 (HY000): invalid input: invalid datetime value 0000-00-00
  3. mysql> SELECT TO_SECONDS('0000-01-01');
  4. ERROR 20301 (HY000): invalid input: invalid datetime value 0000-01-01

Similar to the TO_DAYS() function, for example, when querying SELECT TO_SECONDS('08-10-07');, MatrixOne automatically fills the year 08 to 0008, which is different from MySQL. For more information, see Two-Digit Years in Dates.

Syntax

  1. > TO_SECONDS(expr)

expr is a date or datetime value and can be of type DATETIME, DATE, or TIMESTAMP.

Examples

  1. mysql> SELECT TO_SECONDS('0001-01-01');
  2. +------------------------+
  3. | to_seconds(0001-01-01) |
  4. +------------------------+
  5. | 31622400 |
  6. +------------------------+
  7. 1 row in set (0.00 sec)
  8. mysql> SELECT TO_SECONDS('2023-07-12 08:30:00');
  9. +---------------------------------+
  10. | to_seconds(2023-07-12 08:30:00) |
  11. +---------------------------------+
  12. | 63856369800 |
  13. +---------------------------------+
  14. 1 row in set (0.00 sec)
  15. mysql> SELECT TO_SECONDS('2007-10-07');
  16. +------------------------+
  17. | to_seconds(2007-10-07) |
  18. +------------------------+
  19. | 63358934400 |
  20. +------------------------+
  21. 1 row in set (0.00 sec)
  22. mysql> SELECT TO_SECONDS('97-10-07');
  23. +----------------------+
  24. | to_seconds(97-10-07) |
  25. +----------------------+
  26. | 3085257600 |
  27. +----------------------+
  28. 1 row in set (0.00 sec)