SQL Cookbook Lesson

How to get the current date and time in SQL

Learn how to get the current date and time in SQL with examples and explanations from SQLPad.

## Problem
Your task is to retrieve the current date and time in SQL.

## Sample Data
Since the problem involves retrieving the system's current date and time, we don't need any specific sample data.

## MySQL Solution
```sql
SELECT NOW() AS CurrentDateTime;

MySQL Explanation

In MySQL, the NOW() function is used to fetch the current date and time. The AS keyword is used to rename the output column as "CurrentDateTime".

PostgreSQL Solution

SELECT CURRENT_TIMESTAMP AS CurrentDateTime;

PostgreSQL Explanation

In PostgreSQL, the CURRENT_TIMESTAMP function is used to fetch the current date and time. Similar to the MySQL solution, the AS keyword is used to rename the output column as "CurrentDateTime". It's worth noting that CURRENT_TIMESTAMP provides the date and time in the timezone set in your database system. If you want to get the date and time in UTC, you can use CURRENT_TIMESTAMP AT TIME ZONE 'UTC'. ```

Keep Practicing

Related Cookbook Lessons

View all lessons

Interview Prep

Begin Your SQL, Python, and R Journey

Master 230 interview-style coding questions and build the data skills needed for analyst, scientist, and engineering roles.