SQL Server GETDATE Function

Summary: in this tutorial, you will learn how to use the SQL Server GETDATE() function to get the current system timestamp.

SQL Server GETDATE() function overview

The GETDATE() function returns the current system timestamp as a DATETIME value without the database time zone offset. The DATETIME value is derived from the Operating System (OS) of the server on which the instance of SQL Server is running.

The following shows the syntax of the GETDATE() function:

GETDATE()
</code>Code language: HTML, XML (xml)

Note the the GETDATE() is a nondeterministic function, therefore, you cannot create an index for columns that reference this function in the Views.

SQL Server GETDATE() examples

Let’s take some examples of using the GETDATE() function.

A) Using SQL Server GETDATE() function to get the current system date and time example

This example uses the GETDATE() function to return the current date and time of the OS on which the SQL Server is running:

SELECT 
    GETDATE() current_date_time;
Code language: SQL (Structured Query Language) (sql)

Here is the output:

current_date_time
-----------------------
2019-04-28 15:13:26.270

(1 row affected)
Code language: SQL (Structured Query Language) (sql)

B) Using SQL Server GETDATE() function to get the current system date example

To get the current date, you can use the CONVERT() function to convert the DATETIME value to a DATE as follows:

SELECT 
    CONVERT(DATE, GETDATE()) [Current Date];
Code language: SQL (Structured Query Language) (sql)

The following shows the output:

Current Date
------------
2019-04-28

(1 row affected)
Code language: SQL (Structured Query Language) (sql)

Similarly, you can use the TRY_CONVERT() and CAST() functions to convert the result of the GETDATE() function to a date:

SELECT 
    TRY_CONVERT(DATE, GETDATE()),
    CAST(GETDATE() AS DATE);
Code language: SQL (Structured Query Language) (sql)

C) Using SQL Server GETDATE() function to get the current system time example

To get the current time only, you can use the CONVERT(), TRY_CONVERT(), or CAST() function to convert the result of the GETDATE() function to a time:

SELECT 
    CONVERT(TIME,GETDATE()),
    TRY_CONVERT(TIME, GETDATE()),
    CAST(GETDATE() AS TIME);
Code language: SQL (Structured Query Language) (sql)

In this tutorial, you have learned how to use the SQL Server GETDATE() function to return the current system date and time.

Was this tutorial helpful?