SQL Server Get Current Date

Greetings, Dev! Are you looking to retrieve the current date in SQL Server? You’re in luck because SQL Server has a built-in function that makes it easy to get the current date. In this article, we’ll explore how to use the GETDATE() function in SQL Server to retrieve the current date and time.

What is the GETDATE() Function?

The GETDATE() function is a date and time function in SQL Server that returns the current date and time of the system on which the SQL Server instance is installed. The function has no parameters and returns a datetime data type value.

Here’s an example of how to use the GETDATE() function:

Query
Result
SELECT GETDATE();
2022-01-01 12:00:00.000

Using GETDATE() in SELECT Statements

You can use the GETDATE() function in SELECT statements to retrieve the current date and time. Here’s an example:

Query
Result
SELECT GETDATE() AS CurrentDateTime;
2022-01-01 12:00:00.000

In the example above, we’re using the AS keyword to give the result a column alias. This makes the column more readable in the output.

Formatting the Date

If you want to format the date returned by the GETDATE() function, you can use the CONVERT() function. This function allows you to convert a datetime value to a different format using a style code.

Here’s an example of how to use the CONVERT() function to format the current date:

Query
Result
SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS CurrentDate;
01/01/2022

In the example above, we’re using the style code 101 to format the date as MM/DD/YYYY. You can find a list of style codes in the Microsoft documentation.

Using GETDATE() in WHERE Clauses

You can also use the GETDATE() function in WHERE clauses to filter data based on the current date and time. Here’s an example:

Query
Result
SELECT * FROM Orders WHERE OrderDate > GETDATE();
Returns all orders where the order date is after the current date and time.

FAQ

What is the data type of the value returned by the GETDATE() function?

The GETDATE() function returns a datetime data type value.

Can I use GETDATE() in a stored procedure?

Yes, you can use the GETDATE() function in a stored procedure just like you would use it in any other SQL statement.

Is there an equivalent function for getting only the current time?

Yes, you can use the GETDATE() function in conjunction with the DATEPART() function to extract the time portion of the datetime value. Here’s an example:

READ ALSO  How to Change Name in SQL Server: A Comprehensive Guide for Devs
Query
Result
SELECT DATEPART(HOUR, GETDATE()) AS CurrentHour, DATEPART(MINUTE, GETDATE()) AS CurrentMinute, DATEPART(SECOND, GETDATE()) AS CurrentSecond;
Returns the current hour, minute, and second.

Conclusion

We hope this article has helped you understand how to use the GETDATE() function in SQL Server to retrieve the current date and time. With this function, you can easily retrieve the current date and time, format its output, and use it in WHERE clauses to filter data. If you have any questions or comments, please leave them below.