0
0
MysqlHow-ToBeginner · 3 min read

How to Use CURTIME() in MySQL: Syntax and Examples

In MySQL, use the CURTIME() function to get the current time in 'HH:MM:SS' format. You can call it directly in a query like SELECT CURTIME(); to retrieve the current time from the database server.
📐

Syntax

The CURTIME() function returns the current time as a string in the format 'HH:MM:SS' or as a time value. It takes no arguments.

  • CURTIME(): Returns the current time.
sql
SELECT CURTIME();
Output
12:34:56
💻

Example

This example shows how to get the current time using CURTIME() and how to use it in a table query to compare times.

sql
SELECT CURTIME() AS current_time;

-- Using CURTIME() in a WHERE clause example
CREATE TEMPORARY TABLE appointments (
  id INT,
  appointment_time TIME
);

INSERT INTO appointments VALUES
(1, '09:00:00'),
(2, '15:30:00'),
(3, '23:45:00');

SELECT * FROM appointments WHERE appointment_time > CURTIME();
Output
current_time 12:34:56 id | appointment_time 2 | 15:30:00 3 | 23:45:00
⚠️

Common Pitfalls

Common mistakes when using CURTIME() include:

  • Expecting a date and time: CURTIME() returns only the time, not the date. Use NOW() for date and time.
  • Using parentheses incorrectly: CURTIME must be called with parentheses (), even though it takes no arguments.
  • Comparing with strings without proper format: Time values should be in 'HH:MM:SS' format.
sql
/* Wrong: Missing parentheses */
SELECT CURTIME;

/* Right: With parentheses */
SELECT CURTIME();
Output
ERROR: Unknown column 'CURTIME' in 'field list' -- and -- current_time 12:34:56
📊

Quick Reference

FunctionDescriptionReturns
CURTIME()Returns current time'HH:MM:SS' string or TIME value
NOW()Returns current date and time'YYYY-MM-DD HH:MM:SS' datetime
CURRENT_TIME()Synonym for CURTIME()'HH:MM:SS' string or TIME value

Key Takeaways

Use CURTIME() with parentheses to get the current time in MySQL.
CURTIME() returns only the time, not the date or datetime.
Use NOW() if you need both date and time together.
CURTIME() output format is 'HH:MM:SS', suitable for time comparisons.
Avoid missing parentheses or mixing CURTIME() with date functions.