June 29, 2021

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

About the authorĀ 

Kris Wenzel

Kris Wenzel has been working with databases over the past 30 years as a developer, analyst, and DBA. He has a BSE in Computer Engineering from the University of Michigan and a MBA from the University of Notre Dame. Kris has written hundreds of blog articles and many online courses. He loves helping others learn SQL.

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

Are YOU READY TO START LEARNING SQL SERVER, BUT DON'T KNOW WHERE TO START?

Nothing is worse than, being excited to learn a new tool but not knowing where to start, wasting time learning the wrong features, and being overwhelmed .

But it doesn't have to be this way.

I'm Putting together a free email course to help you get started learning SQL Server.

Name*
Email*
>