Getting the Current Date and Time in SQL

Introduction

Retrieving the current date and time is a common task in SQL. Whether you need to log timestamps, filter records by the current date, or calculate durations, knowing how to get the current date and time in your SQL queries is essential. Different SQL database management systems (DBMS) provide various functions to achieve this. This blog will cover how to get the current date and time in MySQL, PostgreSQL, SQL Server, Oracle, SQLite, and MariaDB.

SQL Functions to Get Current Date and Time

1. MySQL

In MySQL, you can use the following functions to get the current date and time:

  • Current Date: CURDATE()
  • Current Time: CURTIME()
  • Current Date and Time: NOW()

Example:

SELECT CURDATE() AS CurrentDate, CURTIME() AS CurrentTime, NOW() AS CurrentDateTime;

Explanation:

  • CURDATE() returns the current date.
  • CURTIME() returns the current time.
  • NOW() returns the current date and time.

2. PostgreSQL

PostgreSQL provides the following keywords and functions:

  • Current Date: CURRENT_DATE
  • Current Time: CURRENT_TIME
  • Current Date and Time: CURRENT_TIMESTAMP or NOW()

Example:

SELECT CURRENT_DATE AS CurrentDate, CURRENT_TIME AS CurrentTime, CURRENT_TIMESTAMP AS CurrentDateTime;

Explanation:

  • CURRENT_DATE returns the current date.
  • CURRENT_TIME returns the current time.
  • CURRENT_TIMESTAMP or NOW() returns the current date and time.

3. SQL Server

In SQL Server, you can use:

  • Current Date: CAST(GETDATE() AS DATE)
  • Current Time: CAST(GETDATE() AS TIME)
  • Current Date and Time: GETDATE()

Example:

SELECT CAST(GETDATE() AS DATE) AS CurrentDate, CAST(GETDATE() AS TIME) AS CurrentTime, GETDATE() AS CurrentDateTime;

Explanation:

  • CAST(GETDATE() AS DATE) returns the current date.
  • CAST(GETDATE() AS TIME) returns the current time.
  • GETDATE() returns the current date and time.

4. Oracle

In Oracle, you can use:

  • Current Date and Time: SYSDATE or CURRENT_TIMESTAMP

Example:

SELECT SYSDATE AS CurrentDateTime FROM dual;
-- or
SELECT CURRENT_TIMESTAMP AS CurrentDateTime FROM dual;

Explanation:

  • SYSDATE returns the current date and time.
  • CURRENT_TIMESTAMP also returns the current date and time but with more precision.

5. SQLite

In SQLite, you can use:

  • Current Date and Time: datetime('now')

Example:

SELECT datetime('now') AS CurrentDateTime;

Explanation:

  • datetime('now') returns the current date and time in UTC.

6. MariaDB

In MariaDB, you use:

  • Current Date and Time: NOW()

Example:

SELECT NOW() AS CurrentDateTime;

Explanation:

  • NOW() returns the current date and time.

Conclusion

Understanding how to retrieve the current date and time across different SQL database systems is crucial for various data manipulation tasks. Each DBMS has its specific functions and keywords for this purpose. By using the examples provided, you can easily incorporate current date and time retrieval into your SQL queries, ensuring your applications and reports are up-to-date.


Feel free to customize or expand upon this blog post based on your specific needs!