SQL GETDATE Function (Transact SQL)

By: Kris Wenzel   |   Updated: March 6, 2022  
Works With: 

The SQL GETDATE function returns the current timestamp of the database as a datetime value.

Description

The current timestamp derives from the operating system on which the SQL server is running. The GetDATE function returns the timestamp in the format YYYY-MM-DD hh:mm:ss:mmm.

SQL GETDATE Usage Notes

The return data type of the GETDATE function is datetime. Use the function anywhere a datetime expression is expected. The GETDATE function is nondeterministic. It returns a different value that is the current date and time whenever it runs, although the database state remains the same. Therefore, you can not index columns that reference GETDATE.

Syntax

GETDATE()

SQL GETDATE Examples

The following example returns the current date and time of the operating system on which the SQL server instance is running.

SELECT GETDATE() system_current_date_time;
/* Answer */
SELECT GETDATE() system_current_date_time;

SELECT GETDATE() system_current_date_time;

The output is a complete timestamp.

We can use the Convert or the Cast function to keep only the date or the time part.

Here is we get the date:

SELECT CONVERT(date, GETDATE()) currentDate; SELECT CAST(GETDATE() AS date) currentDate;
/* Answer */
SELECT CONVERT(date, GETDATE()) currentDate;
SELECT CAST(GETDATE() AS date) currentDate;

And here we convert GETDATE into the current time.

SELECT CONVERT(time, GETDATE()) currentTime; SELECT CAST(GETDATE() AS time) currentTime;
/* Answer */
SELECT CONVERT(time, GETDATE()) currentTime;
SELECT CAST(GETDATE() AS time) currentTime;

See Also

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}