Working with dates is a fundamental aspect of database management and SQL Server provides several functions to handle date and time. Among these, CURRENT_DATE
stands out as a straightforward and efficient way to retrieve the current date. This guide dives deep into the CURRENT_DATE
function in SQL Server, explaining its syntax, usage, and benefits, especially within Azure SQL Database and Azure SQL Managed Instance.
Understanding CURRENT_DATE
in SQL Server
The CURRENT_DATE
function in SQL Server is designed to return the current date of the database system. Importantly, it returns only the date part, without any time component or time zone offset. This makes it incredibly useful when you’re only concerned with the date and want to exclude any time-related information.
Think of CURRENT_DATE
as a simple way to ask your SQL Server: “What day is it today?”. The function then directly provides you with the date, derived from the operating system where the SQL Server Database Engine is running.
It’s crucial to understand that CURRENT_DATE
is specifically available in Azure SQL Database and Azure SQL Managed Instance. While other SQL Server environments offer similar functionalities, CURRENT_DATE
is the ANSI SQL standard equivalent to getting the date portion from functions like GETDATE()
.
To put it in perspective, functions like SYSDATETIME
and SYSUTCDATETIME
offer higher precision, including fractional seconds. SYSDATETIMEOFFSET
even incorporates the system time zone offset. However, if you strictly need just the date, CURRENT_DATE
provides a cleaner and more direct approach. It’s essentially a more concise and readable alternative to using CAST(GETDATE() AS DATE)
.
Syntax of CURRENT_DATE
The syntax for using CURRENT_DATE
is remarkably simple:
CURRENT_DATE
As you can see, the function takes no arguments. You simply call CURRENT_DATE
, and it will return the current date as a date data type.
Practical Examples of CURRENT_DATE
Let’s explore some practical examples to illustrate how CURRENT_DATE
works in SQL Server.
Example A: Basic Usage – Retrieving the Current Date
To get the current system date, you can simply use the following query:
SELECT CURRENT_DATE;
This query will return a result set with a single column showing the current date. For instance, if you run this query on June 26th, 2024, the output will be:
Value |
---|
2024-06-26 |
Example B: Comparing CURRENT_DATE
with other Date Functions
To understand the difference between CURRENT_DATE
and other date and time functions, let’s look at an example that selects several functions together:
SELECT SYSDATETIME(), SYSUTCDATETIME(), CURRENT_TIMESTAMP, GETDATE(), GETUTCDATE(), CURRENT_DATE;
This query will output a result set showcasing different date and time functions. Notice how CURRENT_DATE
only provides the date portion, while others include time and time zone information.
Data type | Value |
---|---|
SYSDATETIME() |
2024-06-26 14:04:21.6172014 |
SYSUTCDATETIME() |
2024-06-26 19:04:21.6172014 |
CURRENT_TIMESTAMP |
2024-06-26 14:04:21.617 |
GETDATE() |
2024-06-26 14:04:21.617 |
GETUTCDATE() |
2024-06-26 19:04:21.617 |
CURRENT_DATE |
2024-06-26 |
Example C: Extracting Date Only from other Date/Time Functions
If you are using a function that returns both date and time, and you need to extract only the date part, you can use CONVERT(DATE, ...)
along with other functions like SYSDATETIME()
, GETDATE()
, etc. This is functionally similar to CURRENT_DATE
but applicable to a wider range of SQL Server environments.
SELECT CONVERT(DATE, SYSDATETIME()), CONVERT(DATE, GETDATE()), CURRENT_DATE;
This query demonstrates how to extract just the date from SYSDATETIME()
and GETDATE()
and compares the result with CURRENT_DATE
.
Data type | Value |
---|---|
CONVERT(DATE, SYSDATETIME()) |
2024-06-26 |
CONVERT(DATE, GETDATE()) |
2024-06-26 |
CURRENT_DATE |
2024-06-26 |
Important Considerations for CURRENT_DATE
While CURRENT_DATE
is straightforward to use, there are a couple of key points to keep in mind:
- Nondeterministic Function:
CURRENT_DATE
is considered a nondeterministic function. This means that its return value can change each time it is called, even within the same query, depending on the system date. Because of this nondeterministic nature, you cannot index views or expressions that referenceCURRENT_DATE
. - ANSI SQL Standard:
CURRENT_DATE
is part of the ANSI SQL standard, making your SQL code more portable across different database systems that adhere to this standard. - Azure SQL Database and Managed Instance Specific: Remember that
CURRENT_DATE
is specifically available in Azure SQL Database and Azure SQL Managed Instance. If you are working with other versions of SQL Server, you might need to useCAST(GETDATE() AS DATE)
or similar methods to achieve the same result.
Conclusion
CURRENT_DATE
in SQL Server, especially within Azure SQL Database and Azure SQL Managed Instance, offers a clean and efficient way to obtain the current date. Its simple syntax and focus on just the date portion make it ideal for scenarios where time components are irrelevant. By understanding its functionality and limitations, you can effectively utilize CURRENT_DATE
to enhance your SQL queries and date-related operations. Whether you are logging events, filtering data by date, or performing date calculations, CURRENT_DATE
is a valuable tool in your SQL Server toolkit.