How to Use the GETDATE() Function in SQL Server
Grace Collins
Solutions Engineer · Leapcell

Key Takeaways
GETDATE()
returns the current system date and time in SQL Server.- It's commonly used for timestamping, filtering, and date calculations.
GETDATE()
uses the server's local time zone and is non-deterministic.
The GETDATE()
function in SQL Server is a built-in function that returns the current date and time of the system on which the SQL Server instance is running. It's commonly used for timestamping records, filtering data based on the current date, and performing date calculations.
Syntax
GETDATE()
This function does not require any arguments and returns the current date and time as a DATETIME
value in the format YYYY-MM-DD hh:mm:ss.mmm
.
Basic Usage
To retrieve the current date and time:
SELECT GETDATE() AS CurrentDateTime;
This will return the current system date and time.
Common Use Cases
1. Setting Default Values in Tables
You can use GETDATE()
to set default values for date columns when creating tables:
CREATE TABLE Orders ( OrderID INT PRIMARY KEY, OrderDate DATETIME DEFAULT GETDATE() );
This ensures that if no value is provided for OrderDate
during insertion, the current date and time will be used.
2. Filtering Records Based on Current Date
To select records from a table where the date matches the current date:
SELECT * FROM Orders WHERE CAST(OrderDate AS DATE) = CAST(GETDATE() AS DATE);
This query compares only the date parts, ignoring the time components.
3. Calculating Date Differences
To calculate the difference in years between a stored date and the current date:
SELECT EmployeeID, DATEDIFF(YEAR, BirthDate, GETDATE()) AS Age FROM Employees;
This computes the age of each employee based on their birth date.
4. Adding or Subtracting Time Intervals
To add or subtract time intervals from the current date:
-- Add 7 days SELECT DATEADD(DAY, 7, GETDATE()) AS DateAfter7Days; -- Subtract 1 month SELECT DATEADD(MONTH, -1, GETDATE()) AS DateBefore1Month;
These queries adjust the current date by the specified intervals.
Extracting Specific Date Parts
To extract specific parts of the current date:
SELECT YEAR(GETDATE()) AS CurrentYear, MONTH(GETDATE()) AS CurrentMonth, DAY(GETDATE()) AS CurrentDay;
This retrieves the current year, month, and day separately.
Formatting Date and Time
To format the current date and time into a specific string format:
SELECT FORMAT(GETDATE(), 'dd-MM-yyyy HH:mm:ss') AS FormattedDateTime;
This formats the date and time as dd-MM-yyyy HH:mm:ss
.
Considerations
-
Time Zone:
GETDATE()
returns the current date and time based on the server's local time zone. If you need the UTC time, consider usingGETUTCDATE()
. -
Precision: For higher precision (including fractional seconds),
SYSDATETIME()
can be used as it returns aDATETIME2
value with more precision thanGETDATE()
. -
Non-Deterministic Function:
GETDATE()
is non-deterministic, meaning it can return different results each time it's called, even within the same query. This can affect indexing and query optimization.
Conclusion
The GETDATE()
function is a versatile tool in SQL Server for working with the current date and time. Whether you're timestamping records, filtering data, or performing date calculations, GETDATE()
provides a straightforward way to incorporate the current system date and time into your SQL queries.
FAQs
It returns the current date and time of the SQL Server system.
It's used for timestamping records, filtering by current date, and date arithmetic.
No, it provides local server time. Use GETUTCDATE()
for UTC.
We are Leapcell, your top choice for hosting backend projects.
Leapcell is the Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis:
Multi-Language Support
- Develop with Node.js, Python, Go, or Rust.
Deploy unlimited projects for free
- pay only for usage — no requests, no charges.
Unbeatable Cost Efficiency
- Pay-as-you-go with no idle charges.
- Example: $25 supports 6.94M requests at a 60ms average response time.
Streamlined Developer Experience
- Intuitive UI for effortless setup.
- Fully automated CI/CD pipelines and GitOps integration.
- Real-time metrics and logging for actionable insights.
Effortless Scalability and High Performance
- Auto-scaling to handle high concurrency with ease.
- Zero operational overhead — just focus on building.
Explore more in the Documentation!
Follow us on X: @LeapcellHQ