How to Get the Current Date and Time in SQL
To get the current date and time in SQL, use the GETDATE() function. This function takes no arguments and returns the current date and time as a datetime value. For example, if you wanted to get the current date and time, you would use the following query:
SELECT GETDATE();
Examples
Let's look at a few examples of how this function can be used. Suppose you have a table called orders with the following data:
order_id | order_date |
---|---|
1 | 2020-01-01 |
2 | 2020-01-02 |
3 | 2020-01-03 |
If you wanted to get the current date and time and add it to the orders table, you would use the following query:
INSERT INTO orders (order_date) VALUES (GETDATE());
This query would add a new row to the orders table with the current date and time as the order_date value. 🤗
Additional Info
The GETDATE() 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.