SQL SERVER

The Definitive Guide to the SQL Server GETDATE Function

intro

Let's learn everything you need to know about the SQL Server GETDATE function to get the system's current date and time.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

Most technologies come with a function to get the current date and time. That is part of the date utilities provided by the standard API of most programming languages. It is no surprise that SQL Server also has such a built-in feature. This is what the SQL Server GETDATE function is all about!

In this article, you will explore the GETDATE SQL Server function and learn how and when to use it.

Let's dive in!

What Is GETDATE in SQL Server?

GETDATE is an SQL Server function that returns the current timestamp in a timezone specified by default or specified by the user. Specifically, it provides the current database date and time without time zone offset. That reflects the date and time from the operating system of the computer the SQL Server instance runs on.

Note: Since GETDATE is a nondeterministic function, views and expressions referencing this function in a column cannot be indexed.

SQL Server GETDATE Function: Syntax and First Example

Here is the syntax of the SQL Server GETDATE function:

Copy
        
1 GETDATE()

As you can see, it does not accept any argument.

You can use it to retrieve the current date and time of the database server as below:

Copy
        
1 SELECT GETDATE();

The result will be something like this:

Copy
        
1 2024-07-30 15:48:33

Verify that in a fully-featured SQL Server database client like DbVisualizer:

Note the date format in the bottom bar
Note the date format in the bottom bar

Now, you may be wondering about the resulting format of the SQL Server GETDATE function. Thanks to DbVisualizer, you can simply click on the resulting data cell to discover it. You can find out more details about this feature in the documentation.

As indicated in the bottom left bar, GETDATE returns a timestamp in the "yyyy-MM-dd HH:mm" format.

The return data type of GETDATE is DATETIME, which is used in SQL Server to represent timestamps. If you are not familiar with the DATETIME data type, take a look at our guide on SQL date data types.

Use Cases of the GETDATE SQL Server Function

Explore the three most common use cases of GETDATE in SQL Server.

1 — Using It as a Timestamp for Data Insertion

When inserting new records into a table, GETDATE can be used to automatically record the timestamp of when the record was added:

Copy
        
1 INSERT INTO Orders (ProductId, CustomerId, Quantity, OrderedAt) 2 VALUES (14, 1, 2, GETDATE());

Similarly, you can specify as a default value when using a CREATE TABLE query:

Copy
        
1 CREATE TABLE Orders ( 2 OrderID INT PRIMARY KEY, 3 ProductID INT NOT NULL, 4 CustomerID INT NOT NULL, 5 Quantity INT NOT NULL, 6 OrderedAt DATETIME DEFAULT GETDATE() 7 );

2 — Filtering Records by Current Date

The SQL Server GETDATE function makes it easier to filter records based on the current date and time, as in this example:

Copy
        
1 SELECT * 2 FROM Orders 3 WHERE OrderAt < DATEADD(DAY, -1, GETDATE());

This will return all orders placed before yesterday:

Executing the query in DbVisualizer
Executing the query in DbVisualizer

Learn more about DATEADD in our guide on SQL add to date operations.

3 — Calculating Age or Time Difference

GETDATE can also be employed to calculate the difference between the current date and another date, such as when determining the age of a person:

Copy
        
1 SELECT Id, Name, DATEDIFF(YEAR, DateOfBirth, GETDATE()) AS Age 2 FROM Employees;

This relies on the SQL Server DATEDIFF function.

Extra: How To Get Time From GETDATE in SQL Server

The GETDATE SQL Server function returns both data and time, but what if you are interested only in the time component? Since SQL Server does not provide a specific function to get the current time, you can extract that information from GETDATE as explained in the following approaches!

Get the TIME Component

If you want to get the time component from GETDATE, you can convert the resulting DATETIME value to the TIME data type using the SQL CAST operator as below:

Copy
        
1 SELECT CAST(GETDATE() as TIME);

The result will be something like in HH:mm:ss.nnnnnnn format:

Copy
        
1 15:48:33.9900000

Discover more in our guide on how to extract date and time in MS SQL Server.

Get the Time Component as a String

To get the time component from GETDATE as a string, format the resulting DATETIME value with the FORMAT function:

Copy
        
1 SELECT FORMAT(GETDATE(), 'HH:mm:ss');

The result will be a string containing:

Copy
        
1 15:48:33

Get Hours, Minutes, and Seconds

If you need only the hour, minute, or second, you can extract this information with DATEPART as below:

Copy
        
1 SELECT 2 DATEPART(HOUR, GETDATE()) AS CurrentHour, 3 DATEPART(MINUTE, GETDATE()) AS CurrentMinute, 4 DATEPART(SECOND, GETDATE()) AS CurrentSecond;

The result will be:

Executing the query in DbVisualizer
Executing the query in DbVisualizer

Similarly, you can use this approach to get the current year, month, and date from GETDATE.

Note that DbVisualizer makes it easier to write such queries thanks to its SQL editor autocomplete capabilities:

DbVisualizer autocompletion in action
DbVisualizer autocompletion in action

Conclusion

In this guide, you saw what the GETDATE SQL Server function is and how it works. You now know that GETDATE in SQL Server returns the current date and time of the machine the database server is installed on. Thanks to the use cases presented here, you also learned when to use it.

Dealing with date functions and formats becomes easier with a powerful client tool like DbVisualizer. This comprehensive database client supports several DBMS technologies, has advanced query optimization capabilities, and can generate ERD-type schemas with a single click. Try DbVisualizer for free!

FAQ

Is GETDATE part of the ANSI SQL standard?

No, GETDATE is a function that is specifically part of the SQL Server dialect. That means you may not find the SQL Server GETDATE function in other databases. Even if it is available, it might not have the same implementation and could produce different results.

What is the difference between GETDATE and CURRENT_TIMESTAMP in SQL Server?

Both GETDATE and CURRENT_TIMESTAMP return the current database system timestamp as a DATETIME value, without the database time zone offset. The main difference between the two is that GETDATE must be called as a function while CURRENT_TIMESTAMP is a constant. In detail, CURRENT_TIMESTAMP is the ANSI SQL equivalent to GETDATE.

SQL Server GETDATE without time: Is it possible?

While the GETDATE SQL Server function always returns a DATETIME value with both a date and time component, you can use the CAST function to extract only the date:

Copy
        
1 SELECT CAST(GETDATE() AS DATE) AS CurrentDate;

The result will be something like:

Copy
        
1 2024-07-30

What is the difference between the SQL Server GETDATE and GETUTCDATE functions?

The difference between GETUTCDATE and GETDATE in SQL Server is the time zone context they return. GETDATE returns the current date and time based on the server's local time zone, while GETUTCDATE returns the current date and time in UTC.

What is the difference between SYSDATETIME and GETDATE in SQL Server?

The difference between SYSDATETIME and GETDATE in SQL Server is the precision of their results. SYSDATETIME returns the current date and time in the DATETIME2 data type, with precision in nanoseconds, while GETDATE returns the current date and time using the DATETIME data type, with precision in milliseconds.

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

Clustered vs Non-Clustered Index: Complete SQL Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-02-13
title

Queries and Subqueries in SQL: The Definitive Guide

author Lukas Vileikis tags DbVisualizer MySQL POSTGRESQL SQL SQL SERVER 7 min 2025-02-12
title

How to Optimize a Clustered Index Scan in SQL

author Antonello Zanini tags MySQL SQL SQL SERVER 7 min 2025-02-11
title

SQL Derived Table: Everything You Need to Know

author Antonello Zanini tags MySQL POSTGRESQL SQL SQL SERVER 9 min 2025-02-06
title

A Complete Guide to the ALTER TABLE DROP COLUMN Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-01-27
title

How to Use a Recursive CTE in SQL

author Antonello Zanini tags MySQL POSTGRESQL SQL SQL SERVER 5 min 2025-01-22
title

How to Use MERGE in SQL Query Statements: Complete Guide

author Antonello Zanini tags ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-01-20
title

A Guide to SQL Server Indexes on Partitioned Tables

author Antonello Zanini tags SQL SERVER 7 min 2025-01-13
title

How to Drop an Index By Partition Number in SQL Server

author Antonello Zanini tags SQL SERVER 7 min 2025-01-06
title

SQL CHECK Constraint: Definitive Guide With Examples

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-12-16

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.