Back to cookbooks list Articles Cookbook

How to Get the Current Date (Without Time) in T-SQL

  • GETDATE()
  • CAST()

Table of Contents

Problem:

When working with SQL Server, you often need to get the current date without the time component. To achieve this in T-SQL, you can use several straightforward methods. Here, we will show you the most efficient and easiest way.

Solution:

We’ll use the GETDATE() function to get the current date and time. Then we’ll use the CAST() function to convert the returned datetime data type into a date data type.

SELECT CAST( GETDATE() AS Date ) ;

Here’s the result of the query:

2019-08-17

Discussion:

To get the current date and time in SQL Server, use the GETDATE() function. This function returns a datetime data type; in other words, it contains both the date and the time, e.g. 2019-08-20 10:22:34. (Note: This function doesn’t take any arguments, so you don’t have to put anything in the brackets.)

Discover the best interactive SQL Server courses

However, if you want to get just the current date – not the date and the time – you can use the CAST() function. This function takes any expression or any column name as the first argument. Then you use the keyword AS and enter the new data type to return. (The returned value(s) could be column values or the result returned by the expression.)

In our example, the function GETDATE() returned the current date and time as 2019-08-17 20:05:34.540. By using CAST() with GETDATE(), the query above returned only the date 2019-08-17.

Recommended courses:

Recommended articles:

See also: