sql how to get current date

How to Get Current Date in SQL

By David Jackson
AD

Obtaining the current date is a common requirement in SQL queries, often used for filtering records based on timestamps or extracting specific dates from massive datasets. While SQL syntax can vary slightly between different systems, the core functions for retrieving the current date remain consistent. Let's explore how to achieve this using SQL.

Before we delve into the methods, it's essential to understand that SQL doesn't directly deal with time and date data types like some other programming languages. Instead, it stores these values as strings or integers, depending on the specific SQL system. This makes retrieving the current date somewhat unique in SQL.

Using SQL Built-in Functions

Most SQL systems provide built-in functions to extract the current date. These functions return the current date and time, but some systems may have limitations on retrieving only the date. Here's how to use these functions in popular SQL systems:

AD
SQL GETDATE() Explained with Examples | Current Date & Time in SQL Server
SQL GETDATE() Explained with Examples | Current Date & Time in SQL Server
SQL Date & Time Functions: Work with Dates and Timestamps
SQL Date & Time Functions: Work with Dates and Timestamps
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻

MySQL and MariaDB

MySQL and MariaDB provide the NOW function to get the current date and time. To get only the date, you can use the CURDATE function. Here's how:

SELECT CURDATE() AS CurrentDate;

Sql basics for data analytics
Sql basics for data analytics
a whiteboard with the words day 2 retrieving data with selectr on it
a whiteboard with the words day 2 retrieving data with selectr on it
SQL JOINS Explained: A Simple Guide for Data Science Beginners
SQL JOINS Explained: A Simple Guide for Data Science Beginners

PostgreSQL and SQLite

PostgreSQL and SQLite use the CURRENT_DATE function to fetch the current date. Unlike MySQL and MariaDB, using this function in PostgreSQL and SQLite will only return the date, not the time. Here's the syntax:

SELECT CURRENT_DATE AS CurrentDate;

AD

Formatting the Date Output

Dates returned by the functions above might not be in the desired format. To change the date format, you can use formatting functions. Here's how to do it in some popular SQL systems:

How to Find Monthly Revenue Using SQL | SQL for Data Analysts
How to Find Monthly Revenue Using SQL | SQL for Data Analysts
Learn SQL just by investing 30 minutes a day within 30 Days
Learn SQL just by investing 30 minutes a day within 30 Days
30 SQL COMMANDS EVERY DATABASE DEVELOPER SHOULD MASTER
30 SQL COMMANDS EVERY DATABASE DEVELOPER SHOULD MASTER
SQL is one of the most essential skills for aspiring Data Analysts
SQL is one of the most essential skills for aspiring Data Analysts
a poster with the words and instructions for an application to learn how to use it
a poster with the words and instructions for an application to learn how to use it
an info sheet with diagrams and text on it
an info sheet with diagrams and text on it

MySQL and MariaDB

These systems use the DATE_FORMAT function. After retrieving the current date, you can format it like this:

SELECT DATE_FORMAT(NOW(), '%Y-%m-%d') AS CurrentDate;

AD

PostgreSQL and SQLite

In PostgreSQL and SQLite, you can use the TO_CHAR function to format dates. Here's how to format the current date as 'YYYY-MM-DD':

SQL SELECT Explained with Examples | Beginner SQL Guide
SQL SELECT Explained with Examples | Beginner SQL Guide
HOW TO USE SQL CHEAT SHEET IN DATA ANALYST
HOW TO USE SQL CHEAT SHEET IN DATA ANALYST
SQL Cheat Sheet for Beginners
SQL Cheat Sheet for Beginners

SELECT TO_CHAR(CURRENT_DATE, 'YYYY-MM-DD') AS CurrentDate;

The world of SQL is vast and evolving, with different systems and versions offering diverse ways to accomplish the same tasks. Always refer to the official documentation of the SQL system you're using for the most accurate and up-to-date information.

AD
AD