MySQL NOW And SYSDATE Functions: Get Current Time Date

MySQL provides two functions to get the current date and time: NOW() and SYSDATE().

NOW() Function:

The NOW() function is used to return the current date and time.

Syntax:

NOW()

Example:

SELECT NOW();

This will return the current date and time. For example, 2023-05-12 15:45:32.

SYSDATE() Function:

The SYSDATE() function is also used to return the current date and time.

Syntax:

SYSDATE()

Example:

SELECT SYSDATE();

This will also return the current date and time. For example, 2023-05-12 15:45:32.

Difference between NOW() and SYSDATE():

While NOW() and SYSDATE() may seem identical, there is a small but significant difference between them:

  • NOW() function returns the time at which the statement began to execute.
  • SYSDATE() function returns the exact time at which it is executed.

This difference only matters within stored programs (like stored procedures and functions) or within a single query that takes a long time to execute. In most other cases, you can use NOW() and SYSDATE() interchangeably.

Example showing the difference:

SELECT NOW(), SLEEP(5), NOW(), SYSDATE();

In the above query, SLEEP(5) is used to pause execution for 5 seconds. The two NOW() calls will return the exact same time because they're part of the same statement. However, the SYSDATE() function will return the time at the exact moment it is executed, which will be about 5 seconds later than the time returned by NOW().

  1. MySQL NOW function example:

    • The NOW() function in MySQL returns the current date and time.
    SELECT NOW();
    
  2. How to use NOW function in MySQL:

    • To use NOW(), simply include it in your SQL query. It's often used for timestamping records when inserting data.
    INSERT INTO table_name (column1, column2, timestamp_column)
    VALUES ('value1', 'value2', NOW());
    
  3. Getting current timestamp with NOW in MySQL:

    • NOW() returns the current timestamp in the format 'YYYY-MM-DD HH:MM:SS'.
    SELECT NOW() AS current_timestamp;
    
  4. Difference between NOW and SYSDATE in MySQL:

    • NOW() and SYSDATE() both return the current date and time, but NOW() is more widely supported, while SYSDATE() is specific to MySQL.
    SELECT NOW(), SYSDATE();
    
  5. Using NOW for date and time values in MySQL:

    • NOW() can be used to set the timestamp for date and time columns.
    UPDATE table_name SET timestamp_column = NOW() WHERE id = 1;
    
  6. MySQL SYSDATE function example:

    • SYSDATE() is an alternative to NOW() in MySQL and provides the current date and time.
    SELECT SYSDATE();
    
  7. How to use SYSDATE function in MySQL:

    • Usage of SYSDATE is similar to NOW.
    INSERT INTO table_name (column1, column2, timestamp_column)
    VALUES ('value1', 'value2', SYSDATE());
    
  8. Getting system date with SYSDATE in MySQL:

    • SYSDATE() retrieves the system's current date and time.
    SELECT SYSDATE() AS system_date;
    
  9. Comparing current timestamp functions in MySQL: NOW vs SYSDATE:

    • Both functions provide the current date and time, but NOW() is more commonly used due to its broader support.
    SELECT NOW() AS now_result, SYSDATE() AS sysdate_result;