Articles Cookbook
Back to list
SQL Server

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

Database:

Operators:

GETDATE(), CAST()

Problem:

You’d like to get the current date in T-SQL, but you don’t need the time.

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.)

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:

go to top