How to use GETDATE in SQL

How to use GETDATE in SQL

The GETDATE() function is one of the most commonly used date and time functions in SQL Server. It returns the current system timestamp of the server on which the SQL Server instance is running. Whether you’re logging entries, filtering data by time, or performing date calculations, GETDATE() is an essential tool in your SQL toolkit.

This article will walk through the usage of GETDATE() with examples and use cases to help you understand how and when to apply it effectively.

Learn SQL online

For our instructor lead, more in depth look at learning to write a SQL SELECT, SORT BY and WHERE clauses why not check out our video course hosted on UDEMY or enjoy the free SQL course below.

What Is GETDATE ?

The GETDATE() function is a built-in scalar function in SQL Server that returns the current date and time of the server running the database engine. The value returned is of the DATETIME data type, which includes both a date and a time component.

It’s important to note that GETDATE() reflects the date and time according to the server’s system clock. This means that if your database is hosted in a different time zone, the value might differ from your local system’s time.

Syntax

				
					SELECT GETDATE() AS CurrentDateTime;
				
			
  • Parameters: None.

  • Return Type: DATETIME (contains both date and time).

  • Precision: Rounded to increments of .000, .003, or .007 seconds.

This simplicity makes it extremely convenient to use in a wide variety of SQL queries.

Common Use Cases for GETDATE()

The GETDATE() function is versatile and widely used in day-to-day SQL tasks. Below are some common scenarios:

Inserting Timestamps into a Table

A very common use case is inserting the current date and time into a column as part of a logging or audit trail system.

				
					INSERT INTO UserLoginAudit (UserID, LoginTime)
VALUES (1234, GETDATE());
				
			

In this example, when a user logs in, their user ID is logged along with the exact timestamp of their login.

This is ideal for:

  • Tracking user sessions

  • Recording when data was added or updated

  • Monitoring operational events

Filtering Records by the Current Date

Let’s say you want to retrieve all records from a table where a certain date column matches today’s date (ignoring the time part).

				
					SELECT *
FROM Orders
WHERE CAST(OrderDate AS DATE) = CAST(GETDATE() AS DATE);
				
			

Here, we cast both OrderDate and the result of GETDATE() to DATE, which strips out the time component and allows for direct comparison based solely on the date.

Retrieving Records from the Last N Days

To retrieve rows where a date column (e.g., OrderDate) falls within the past 7 days from today, use DATEADD() in combination with GETDATE().

				
					SELECT *
FROM Orders
WHERE OrderDate >= DATEADD(DAY, -7, GETDATE());
				
			

This query subtracts 7 days from the current date and retrieves all orders made within that time window.

Use cases:

  • Recent purchases

  • Active users

  • Time-based KPIs or dashboards

Identifying Upcoming Events or Deadlines

You can also use GETDATE() to find upcoming records—such as subscriptions or contracts that are about to expire.

				
					SELECT *
FROM Subscriptions
WHERE ExpiryDate BETWEEN GETDATE() AND DATEADD(DAY, 30, GETDATE());
				
			

This retrieves all subscriptions that are going to expire within the next 30 days.

Extracting or Formatting the Date and Time from GETDATE()

Sometimes you may only need the date, the time, or a custom-formatted version of the timestamp.

Get Only the Date

				
					SELECT CONVERT(DATE, GETDATE()) AS TodayDate;
				
			

Result:
2025-04-08

Get Only the Time

				
					SELECT CONVERT(TIME, GETDATE()) AS CurrentTime;
				
			

Another useful operator is BETWEEN, which is great for checking if a value falls within a certain range. For instance, if you’re interested in employees who were hired between 2016 and 2019, you might write:

				
					SELECT *
FROM employees
WHERE hire_date BETWEEN '2016-01-01' AND '2019-12-31';
				
			

Result:
14:53:22.8430000

Format the Date and Time

SQL Server provides the FORMAT() function to return a string in a custom format.

				
					SELECT FORMAT(GETDATE(), 'yyyy-MM-dd HH:mm:ss') AS FormattedDateTime;
				
			

Result:
2025-04-08 14:53:22

You can customize this format to match regional preferences, e.g., MM/dd/yyyy, or display names like ddd MMM yyyy.

				
					SELECT *
FROM employees
WHERE department IS NOT NULL;
				
			

Considerations When Using GETDATE()

  • Time Zones: The value returned depends on the server’s time zone. If your application or users operate in different time zones, consider using SYSDATETIMEOFFSET() or storing time in UTC and converting in your application logic.

  • Determinism: GETDATE() is non-deterministic, meaning it returns different results every time it’s called. This can affect functions, views, or computed columns that require deterministic functions.

  • Precision: If you require higher precision (down to 100 nanoseconds), use SYSDATETIME() instead.

Alternatives and Equivalents in Other Databases

GETDATE() is specific to Microsoft SQL Server. If you’re working in a different database system, use the following equivalents:

DatabaseEquivalent Function
SQL ServerGETDATE()
PostgreSQLCURRENT_TIMESTAMP or NOW()
MySQLNOW()
OracleSYSDATE

Summary

The GETDATE() function is a fundamental part of SQL Server and is indispensable when working with time-related data. From logging timestamps to filtering time windows, it’s a reliable and easy-to-use function for many day-to-day data tasks.

Key Takeaways:

  • GETDATE() returns the current system date and time as a DATETIME.

  • Combine it with CAST, CONVERT, FORMAT, and DATEADD to manipulate or format the output.

  • Consider time zone and precision requirements for your application.

  • Useful in a wide range of applications including auditing, scheduling, filtering, and reporting.

Whether you’re building a data pipeline, designing a web app backend, or writing reports, mastering GETDATE() will make your SQL much more dynamic and time-aware.

Please see the other SQL video learning articles below as part of this learning SQL online course video. 

If you would like an instructor lead SQL course that goes into more detail with worked examples then we also provide a video course to learn SQL online hosted on UDEMY. 

Learning SQL programming with SQL server
Learn SQL Online