SQL DATE – Function, Query, Timestamp Example Format (2024)

/ #Sql
SQL DATE – Function, Query, Timestamp Example Format (2)

Dates are an integral part of any programming language, and SQL is no exception. When you insert data into your SQL database, you can add a date and query the database based on that date.

In this article, you’ll learn about DATE functions in SQL and how to query a database with dates. We'll also take a look at some time functions.

What We'll Cover

  • Date Functions in SQL
    • ADDDATE()
    • CURRENT_DATE()
    • CURRENT_TIME();
    • CURRENT_TIMESTAMP();
    • NOW()
    • DATE
    • DATE_SUB
    • DATEDIFF
    • DAY
    • MONTH
    • YEAR
  • How to Query a Database Based on Dates
  • Conclusion

Date Functions in SQL

ADDDATE()

The ADDDATE() function does what the name implies – it adds an interval to a date or datetime.

You can use the ADDDATE() function in this format: ADDDATE(date, INTERVAL value addunit).

  • date is the date you’re working with. For MySQL, the date format is YYY-MM-DD and is required.
  • INTERVAL is a required keyword
  • value is an integer representing the interval you want to add
  • addunit is what the interval should represent. That is year, month, day, hours, minutes, seconds, and other relevant units.

For example, running the query below returns '2022-10-22'. This means that 10 days got added to '2022-10-12'.

SELECT ADDDATE("2022-10-12", INTERVAL 10 DAY);

SQL DATE – Function, Query, Timestamp Example Format (3)

If you want, you can use it with month or year:

SQL DATE – Function, Query, Timestamp Example Format (4)

CURRENT_DATE()

The CURRENT_DATE() function shows exactly what it says – the current date. It returns the date in the YYYY-MM-DD format.

For instance, SELECT CURRENT_DATE() returns the date I started writing this article:
SQL DATE – Function, Query, Timestamp Example Format (5)

CURRENT_TIME();

The CURRENT_TIME function shows the current time.

SELECT CURRENT_TIME();

SQL DATE – Function, Query, Timestamp Example Format (6)

CURRENT_TIMESTAMP();

The current timestamp function returns the current date and time. It’s the combination of CURRENT_DATE() and CURRENT_TIME().

SELECT CURRENT_TIMESTAMP();

NOW()

The NOW() function returns the current date and time.

SELECT NOW();

SQL DATE – Function, Query, Timestamp Example Format (7)

DATE

You can use the DATE function to extract the date part of a timestamp.

SELECT DATE("2022-11-14 12:00:00");

SQL DATE – Function, Query, Timestamp Example Format (8)

DATE_SUB

The DATE_SUB() function subtracts a day, month, or year from a date.
In the query below, I subtracted 10 days from the date I started writing this article:

SELECT DATE_SUB("2022-11-14", INTERVAL 10 DAY);

SQL DATE – Function, Query, Timestamp Example Format (9)

DATEDIFF

The DATEDIFF() function returns the number of days between two dates.

SELECT DATEDIFF("2023-11-14", "2022-11-14");

SQL DATE – Function, Query, Timestamp Example Format (10)

DAY

This function returns the day within a specified date.

SELECT DAY("2022-11-14");

SQL DATE – Function, Query, Timestamp Example Format (11)

MONTH

The MONTH function returns the month in a specified date.

SELECT MONTH("2022-11-14");

SQL DATE – Function, Query, Timestamp Example Format (12)

YEAR

The YEAR function returns the year in a specified date.

SELECT YEAR("2022-11-14");

SQL DATE – Function, Query, Timestamp Example Format (13)

How to Query a Database Based on Dates

To show you how to query a database using dates, I’ll be using the table below:

SQL DATE – Function, Query, Timestamp Example Format (14)

To select some particular date between one date and another, you can use the BETWEEN and AND keywords while specifying the dates.

In the query below, I select all the items added to the database in 2021:

SELECT *FROM brandsWHERE date_added BETWEEN "2021-01-01" AND "2021-12-31";

SQL DATE – Function, Query, Timestamp Example Format (15)

Combining the DATE_SUB() and NOW() functions, I was able to get the items added to the database in the last 3 months:

SELECT *FROM brandsWHERE date_added > DATE_SUB(NOW(), INTERVAL 3 MONTH);

SQL DATE – Function, Query, Timestamp Example Format (16)

Conclusion

This article showed you some important functions you can use to work with dates and query your database within SQL.

If you find the article useful, don’t hesitate to share it with friends and family.

Thanks for reading.

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

SQL DATE – Function, Query, Timestamp Example Format (17)
Kolade Chris

Web developer and technical writer focusing on frontend technologies. I also dabble in a lot of other technologies.

If you read this far, thank the author to show them you care.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

SQL DATE – Function, Query, Timestamp Example Format (2024)
Top Articles
Latest Posts
Article information

Author: Maia Crooks Jr

Last Updated:

Views: 6338

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Maia Crooks Jr

Birthday: 1997-09-21

Address: 93119 Joseph Street, Peggyfurt, NC 11582

Phone: +2983088926881

Job: Principal Design Liaison

Hobby: Web surfing, Skiing, role-playing games, Sketching, Polo, Sewing, Genealogy

Introduction: My name is Maia Crooks Jr, I am a homely, joyous, shiny, successful, hilarious, thoughtful, joyous person who loves writing and wants to share my knowledge and understanding with you.