Skip to main content

Use time functions in queries

This topic provides examples of how to use time functions in queries.

Time functions

seekdb supports the following time types: DATE, TIMESTAMP, TIME, DATETIME, and YEAR. seekdb also provides the following commonly used time functions: NOW(), CURDATE(), and CURTIME().

Examples

Change the display format of a time type

In seekdb, you can use the NOW() function to output the current time, and the DATE_FORMAT function to change the display format of a time type.

SELECT NOW(),DATE_FORMAT(NOW(),"%Y/%M/%D %T") new_time;
+---------------------+-----------------------------+
| NOW() | new_time |
+---------------------+-----------------------------+
| 2025-12-23 18:15:36 | 2025/December/23rd 18:15:36 |
+---------------------+-----------------------------+
1 row in set

Extract the year, month, day, hour, minute, and second from a time value

In seekdb, you can use the EXTRACT() function to extract the year, month, day, hour, minute, and second from a time value.

SET @dt = NOW();
Query OK, 0 rows affected

SELECT @dt
, EXTRACT(YEAR FROM @dt) d_year
, EXTRACT(MONTH FROM @dt) d_month
, EXTRACT(week FROM @dt) d_week
, EXTRACT(DAY FROM @dt) d_day
, EXTRACT(HOUR FROM @dt) d_hour
, EXTRACT(MINUTE FROM @dt) d_min
, EXTRACT(SECOND FROM @dt) d_second
, EXTRACT(year_month FROM @dt) d_year_month
, EXTRACT(hour_minute FROM @dt) d_hour_min
\G

*************************** 1. row ***************************
@dt: 2025-12-23 18:15:53
d_year: 2025
d_month: 12
d_week: 51
d_day: 23
d_hour: 18
d_min: 15
d_second: 53
d_year_month: 202512
d_hour_min: 1815
1 row in set

Perform addition and subtraction operations on a time value

In seekdb, you can use the DATE_ADD() or DATE_SUB() function to perform addition and subtraction operations on a time value. Here is an example:

SET @dt = NOW();
Query OK, 0 rows affected

SELECT @dt
, DATE_ADD(@dt, INTERVAL 1 DAY ) t1
, DATE_ADD(@dt, INTERVAL 1 HOUR ) t2
, DATE_ADD(@dt, INTERVAL -10 MINUTE ) t3
, DATE_ADD(@dt, INTERVAL -1 MONTH ) t4
, DATE_ADD(@dt, INTERVAL 1 YEAR ) t5
\G

*************************** 1. row ***************************
@dt: 2025-12-23 18:16:11
t1: 2025-12-24 18:16:11
t2: 2025-12-23 19:16:11
t3: 2025-12-23 18:06:11
t4: 2025-11-23 18:16:11
t5: 2026-12-23 18:16:11
1 row in set

For more information about time types, see the SQL Reference.