How to use the MySQL SYSDATE() function

In this article, we will learn how to use the MySQL SYSDATE() function, which returns the current date and time as a datetime value.

Posted on

In this article, we will learn how to use the MySQL SYSDATE() function, which returns the current date and time as a datetime value. We will also see some examples of how to use this function in different situations, and explore some related functions that can be helpful for working with dates and times.

Syntax

The syntax of the SYSDATE() function is as follows:

SYSDATE()

The SYSDATE() function does not take any parameters. The function returns a datetime value that represents the current date and time at the time of execution. The format of the datetime value is ‘YYYY-MM-DD HH:MM:SS’. For example, SYSDATE() might return ‘2023-01-15 10:02:34’, depending on the current date and time.

Examples

Let’s see some examples of how to use the SYSDATE() function in MySQL.

Example 1: Get the current date and time

We can use the SYSDATE() function to get the current date and time as a datetime value. For example:

SELECT SYSDATE() AS result;

This query will return the current date and time as a datetime value. For example, the query might return ‘2023-01-15 10:02:34’, depending on the current date and time.

Example 2: Get the current date

We can use the SYSDATE() function with the DATE() function, which returns the date part of a date or datetime value, to get the current date as a date value. For example:

SELECT DATE(SYSDATE()) AS result;

This query will return the current date as a date value. For example, the query might return ‘2023-01-15’, depending on the current date.

Example 3: Get the current time

We can use the SYSDATE() function with the TIME() function, which returns the time part of a date or datetime value, to get the current time as a time value. For example:

SELECT TIME(SYSDATE()) AS result;

This query will return the current time as a time value. For example, the query might return ‘10:02:34’, depending on the current time.

Example 4: Get the current year

We can use the SYSDATE() function with the YEAR() function, which returns the year of a date or datetime value, to get the current year as an integer value. For example:

SELECT YEAR(SYSDATE()) AS result;

This query will return the current year as an integer value. For example, the query might return 2023, depending on the current year.

Example 5: Get the current month

We can use the SYSDATE() function with the MONTH() function, which returns the month of a date or datetime value, to get the current month as an integer value. For example:

SELECT MONTH(SYSDATE()) AS result;

This query will return the current month as an integer value. For example, the query might return 1, depending on the current month.

There are some other functions that are related to the SYSDATE() function, and can be useful for working with dates and times. Here are some of them:

  • NOW(): This function returns the current date and time as a datetime value. This function is equivalent to the SYSDATE() function. For example, NOW() returns ‘2023-01-15 10:02:34’, depending on the current date and time.
  • CURDATE(): This function returns the current date as a date value. This function is equivalent to the DATE(SYSDATE()) function. For example, CURDATE() returns ‘2023-01-15’, depending on the current date.
  • CURTIME(): This function returns the current time as a time value. This function is equivalent to the TIME(SYSDATE()) function. For example, CURTIME() returns ‘10:02:34’, depending on the current time.
  • DATE(): This function returns the date part of a date or datetime value. For example, DATE('2023-01-15 10:02:34') returns ‘2023-01-15’.
  • TIME(): This function returns the time part of a date or datetime value. For example, TIME('2023-01-15 10:02:34') returns ‘10:02:34’.

Conclusion

In this article, we learned how to use the MySQL SYSDATE() function, which returns the current date and time as a datetime value. We also saw some examples of how to use this function in different situations, and explored some related functions that can be helpful for working with dates and times.