How to Get the Current Date in SQL?
To get just the current date in SQL, a multitude of different functions could be used. The GETDATE() function, SYSDATETIME() function, and CURRENT_TIMESTAMP() function is a few of the in-built functions that SQL could leverage. These are used to display the current date or time or both of the systems.
In this article, we talk about each of these functions in detail.
To get the current date in SQL, the GETDATE() function is the most commonly used function.
However, it is important to note that the GETDATE() function for retrieving the current date in SQL returns both the current date and time. Therefore, to print just the date, the CAST() function could be used to cast away the time returned and display only the date. It converts the datetime data type into just the date data type.
Insert Current Date in SQL
To insert the value of the current date in SQL, we can use the curdate() method.
First, a table needs to be created to retrieve the datetime column :
Now, that the table has been created, the curdate() method could be used to push the values of the system's current date and time into the table InsertDate :
Now to obtain the output, all one has to do is query the DateTodayrecord from the table.
Example Using Current Date in SQL
The pre-defined SQL functions GETDATE(), CURRENT_TIMESTAMP, and SYSDATETIME() return output values in the datetime format. The CAST() function could be used only to cast away the time values and produce strictly date values.
The output for this code is going to be the current date.
The CURRENT_TIMESTAMP() function could be used interchangeably with the GETDATE() function.
This is also going to display both the date and the time together. So, the CAST() function may be called again.
The SYSDATETIME() function could also be used to return the date and time of the system.
- The current date in SQL could be used to retrieve the date values in SQL.
- A number of functions such as GETDATE(), SYSDATETIME(), CURDATE(), CURRENT_TIMESTAMEP() could be used to obtain the date values.
- All these functions could be used interchangeably.
- The CAST() function could be used to change the datetime outputs to only dates.
- The current date in SQL could also be inserted into the system by creating a table and inserting curdate() values into it.