How to Get the Current Date in SQL
The Solution
The solution is to use the CURRENT_DATE function. This function takes no arguments and returns the current date in the format YYYY-MM-DD. For example, if you wanted to get the current date, you would use the following query:
SELECT CURRENT_DATE;
Examples
Let's look at a few examples of how this function can be used. Suppose you have a table called users with the following data:
name | created_at |
---|---|
John | 2020-01-01 |
Jane | 2020-02-01 |
John | 2020-03-01 |
Bob | 2020-04-01 |
If you wanted to get the current date and compare it to the created_at column, you would use the following query:
SELECT CURRENT_DATE, created_at FROM users;
This query would return the result 2020-05-01, 2020-01-01, 2020-05-01, 2020-02-01, 2020-05-01, 2020-03-01, and 2020-05-01, 2020-04-01, showing the current date compared to the date each user was created.
Additional Info
The CURRENT_DATE function is supported by most major databases, including MySQL, PostgreSQL, and SQL Server. However, the syntax may vary slightly depending on the database you are using. For more information, check out the documentation for your particular database. 🤓Do you need to get the current date in SQL? Whether you're trying to track when a record was created or updated, or you're just curious about the current date, SQL has a handy way of getting the current date.