How to use the MySQL CURDATE() function

The CURDATE() function in MySQL returns the current date in ‘YYYY-MM-DD’ format. It is useful for inserting and comparing against the current date.

Posted on

The CURDATE() function in MySQL returns the current date in ‘YYYY-MM-DD’ format. It is useful for inserting and comparing against the current date.

Syntax

The syntax for CURDATE() is simple:

CURDATE()

It takes no arguments.

Examples

  1. Insert current date into a table:

    INSERT INTO purchases(purchase_date)
    VALUES (CURDATE());
    

    This inserts the current date into the purchase_date column.

  2. Select records where date field is current date:

    SELECT * FROM appointments WHERE appt_date = CURDATE();
    

    This returns records where the appointment date is today’s date.

  3. Compare current date to a specified date:

    SELECT CURDATE() = '2023-11-09';
    

    This compares the current date to the specified date.

  4. Use CURRENT_DATE as alias for CURDATE():

    SELECT CURRENT_DATE();
    

    Both CURRENT_DATE() and CURDATE() return the current date.

  5. Get the current date and time together:

    SELECT CONCAT(CURDATE(), ' ', CURTIME());
    

    This returns current date and time together.

Other Similar Functions

  • CURRENT_DATE() - Alias for CURDATE()
  • NOW() - Current date and time
  • CURTIME() - Current time
  • UNIX_TIMESTAMP() - Unix timestamp

So CURDATE() provides an easy way to use the current date in queries, inserts, and comparisons in MySQL.