How to Get the Current Date in SQL
An important thing to know when writing queries is how to get the current date in SQL. Fortunately, this is an easy topic to learn, but it has slight variations across the different types of database platforms. Some databases have different ways of getting the current date than others do.
We’ll take a look at three of the most common database platforms to see how each one allows you to get the current date. We will look at Microsoft SQL Server, MySQL, and PostgreSQL.
Get the Current Date in Microsoft SQL Server
To get the current date in Microsoft SQL Server, you will use a function that SQL Server providers. This is a built-in function which means that it is available to all users without having to do anything extra.
The name of the function is
GETDATE. It’s effortless to use this function. You don’t even have to have a
FROM clause in your query. You can simply run the following query. (Note that the function name is NOT case sensitive so
getdate() have the same result.)
That’s it! If you run this query in your SQL environment, you’ll see the current date. Note that the date and time are based on the server settings. If your server is set to the Eastern Time Zone, it will return the date and time in that time zone, even if you are in the Pacific Time Zone or some other location. So always remember that when working with dates in a database.
Get the Current Date in MySQL or PostgreSQL
MySQL and PostgreSQL do not provide a function called
GETDATE. Instead, they provide a function called
NOW. This function operates in exactly the same way as
GETDATE does in Microsoft SQL Server – it returns the current date and time in the time zone that the database server is configured to. You can use it in exactly the same way though.
It’s as easy as that! But there are a few other things to know about getting the current date in SQL and how you can use it.
Updating Data with the Current Date in SQL
You can update a column in a table to use the current date in SQL. All you have to do is use the appropriate date function on the right side of the equals operator in your update query.
UPDATE people SET last_updated_date = now();
This query would work in either MySQL or PostgreSQL. If you want to run it in MS SQL Server, you would simply change
getdate(). Since we don’t have any criteria, this would set the value of the
last_updated_date column for all rows to the current date.
It’s also good to know that when you define your tables, you can use
getdate() as a default value. This is helpful if you want to keep track of the date and time when a record was created. You could have a column called
created_date and set the default to be
now() (for MySQL or PostgreSQL) or
getdate() (for MS SQL Server) and every time you create a record it will automatically populate the current date as long as you don’t provide any other value for that column in your insert statement.
Selecting the Current Date in SQL Queries
If you want to include the current date as a column in your SQL queries, you can simply include the date function as a “column” in your
SELECT first_name, last_name, email_address, getdate() AS current_date FROM people ORDER BY first_name, last_name
Note that we’re using an alias via the
AS keyword. That way when our results are returned, we have a name for the column instead of the default identifier that the database may assign.
More About Using Dates in SQL
Each database platform provides different ways and functions for dealing with dates. Consider reviewing the functions available within the database documentation for the one that you’re using to learn more. Here are some quick links to get you started.
- MySQL Date and Time Functions
- PostgreSQL Date and Time Functions
- Microsoft SQL Server Date and Time Functions
Want to learn more about SQL? Try our SQL beginner course today to get up and running in no time.
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?