Introduction
In this article, we will learn different ways to get today's date in SQL Server. SQL is a language used to communicate with data stored in a database and manage the data stored in a database. We will learn different ways to find the current date using special ways or commands called functions. These functions are built into SQL and are easy to use. We will also give examples of how to use these functions. This article is good for people who are new to SQL or want to learn more about getting the current date.
Using the GETDATE() Function
The most straightforward way to get the current date in SQL Server is using the built-in function GETDATE(). This function returns the current date and time in the format 'yyyy-mm-dd hh:mm:ss'. Here's an example of how to use GETDATE() in a query.
SELECT GETDATE();
Output to the above SQL statement is shown below in Figure 1.
Using the SYSDATETIME() Function
Another way to get the current date and time in SQL Server is by using the SYSDATETIME() function. This function returns the current system date and time as a datetime value. Here's an example of how to use SYSDATETIME() in a query.
SELECT SYSDATETIME();
Output to the above SQL statement is shown below in Figure 2.
Using the CURRENT_TIMESTAMP Function
You can also use CURRENT_TIMESTAMP function to get the current date and time in SQL Server. This function returns the current date and time in the format 'yyyy-mm-dd hh:mm:ss'. Here's an example of how to use CURRENT_TIMESTAMP in a query.
SELECT CURRENT_TIMESTAMP;
Output to the above SQL statement is shown below in Figure 3.
Using DATEADD (day,0,GETDATE()) function
You can also use these functions with other functions and operators to perform calculations or comparisons with the current date.
Select DATEADD (day,0,GETDATE());
Output is above SQL statement is shown below in Figure 4.
Using SELECT CONVERT(date, GETDATE()) function
You can also use these functions with other functions to get the current date in SQL Server by using the CONVERT() function to convert the GETDATE() function to a date data type.
SELECT CONVERT(date, GETDATE());
Output to the above SQL statement is shown below in Figure 5.
Using SELECT CAST(GETDATE() as date) function
You can also use these functions with other functions to get the current date in SQL Server by using the CAST() function to convert the GETDATE() function to a date data type.
SELECT CAST(GETDATE() as date);
Output to the above SQL statement is shown below in Figure 6.
Conclusion
Here, we have learned different ways to get today's date in SQL Server.
If you require any clarification/suggestions on the article, please leave your questions and thoughts in the comment section below. Follow C# Corner to learn more new and amazing things about SQL or to explore more technologies.
Thanks for reading, and I hope you like it.