Do You Want to Learn SQL?

Take our free eight day course, where I'll teach you in simple to understand English all you need to get started learning SQL.

-
DAYS
-
HOURS
-
MINUTES
-
SECONDS

Free Beginners SQL Course!

SQL GETDATE Function (Transact SQL)

·

·

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

More from the blog


MySQL PostgreSQL SQLite SQL Server