Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Server CURRENT_DATE: How to Get Today's Date

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

SQL Server does not have a built-in CURRENT_DATE function like some other database systems (for example, MySQL or PostgreSQL). Instead, you use GETDATE() or SYSDATETIME() to get the current date and time, and then cast or convert the result to a date-only value. The simplest working approach is SELECT CAST(GETDATE() AS date); which returns today's date without the time component. This guide explains how to get the current date in SQL Server, how to use it in queries, and how to avoid common pitfalls.

What You'll Need

  • Access to a SQL Server instance (any recent version, including Express, Standard, or Enterprise).
  • A SQL client such as SQL Server Management Studio (SSMS), Azure Data Studio, or sqlcmd.
  • Permission to run SELECT statements in the database you are using.

Step-by-Step

  1. Open your SQL client and connect to the database. In SSMS, enter the server name, choose authentication, and click Connect. In Azure Data Studio, create a new connection with the same details. If you are using sqlcmd, run sqlcmd -S server_name -U username -P password and then USE your_database;.
  2. Run a simple query to get the current date and time. Execute SELECT GETDATE(); This returns a datetime value that includes both date and time, based on the server's local time zone. For higher precision, use SELECT SYSDATETIME(); which returns a datetime2 value.
  3. Convert the result to a date-only value. Use CAST or CONVERT to strip the time portion: SELECT CAST(GETDATE() AS date); or SELECT CONVERT(date, GETDATE()); Both return a date data type with no time component.
  4. Use the current date in a WHERE clause. For example, to find orders placed today: SELECT * FROM Orders WHERE OrderDate = CAST(GETDATE() AS date); Note that if OrderDate includes a time, this exact match may miss rows; see Common Mistakes below.
  5. Get the current date in a specific format. If you need a string like 'YYYY-MM-DD', use SELECT CONVERT(varchar(10), GETDATE(), 23); Style 23 gives ISO format. For other formats, refer to the CONVERT style chart in SQL Server documentation.
  6. Use CURRENT_TIMESTAMP as an ANSI-compliant alternative. SELECT CURRENT_TIMESTAMP; is equivalent to GETDATE() and returns a datetime value. It is not a date-only function, but it is standard SQL and works in SQL Server.
  7. Consider time zone differences. GETDATE() uses the server's local time. If you need UTC, use GETUTCDATE() or SYSUTCDATETIME(). For example, SELECT CAST(GETUTCDATE() AS date); returns the current UTC date.
  8. Test your query with sample data. Run a SELECT that includes both the current date and a date column from your table to verify how they compare. Adjust your logic if time components are present.

Common Mistakes

  • Assuming CURRENT_DATE exists. SQL Server does not support CURRENT_DATE; using it will raise an error. Use CAST(GETDATE() AS date) instead.
  • Comparing a date column with a datetime value. If your column stores time, WHERE OrderDate = CAST(GETDATE() AS date) will only match rows at midnight. Use a range: WHERE OrderDate >= CAST(GETDATE() AS date) AND OrderDate < DATEADD(day, 1, CAST(GETDATE() AS date)).
  • Forgetting about time zones. GETDATE() returns server local time, which may differ from your application's time zone. Use GETUTCDATE() or SYSDATETIMEOFFSET() if you need consistency across regions.
  • Using GETDATE() in a function or computed column without considering determinism. GETDATE() is non-deterministic, so it cannot be used in indexed views or persisted computed columns. Use SYSDATETIME() only in contexts where non-determinism is allowed.

Tips

  • Use CAST(GETDATE() AS date) for a clean date-only value; it is readable and efficient.
  • For date arithmetic, use DATEADD and DATEDIFF with the date type to avoid time component issues.
  • If you frequently need the current date, consider creating a scalar function that returns CAST(GETDATE() AS date) for consistency, but be aware of performance implications in large queries.
  • When formatting dates for display, use FORMAT(GETDATE(), 'yyyy-MM-dd') (SQL Server 2012+) for clarity, but note that FORMAT can be slower than CONVERT.

Troubleshooting

  • Error: 'CURRENT_DATE' is not a recognized built-in function name. This confirms SQL Server does not have CURRENT_DATE. Replace it with CAST(GETDATE() AS date).
  • Query returns no rows when filtering by today's date. Check if the column includes a time component. Use a range condition as shown in Common Mistakes.
  • Date is off by one day. Verify the server's time zone and whether you need UTC. Use GETUTCDATE() or adjust with SWITCHOFFSET.
  • Permission denied. Ensure your login has SELECT permission on the table or database.

Frequently Asked Questions

Is there a CURRENT_DATE function in SQL Server?

No, SQL Server does not have a CURRENT_DATE function. Use CAST(GETDATE() AS date) or CONVERT(date, GETDATE()) to get the current date.

What is the difference between GETDATE() and SYSDATETIME()?

GETDATE() returns a datetime value with millisecond precision (though accuracy is about 3.33 ms). SYSDATETIME() returns a datetime2 value with higher precision (100 ns). Both use the server's local time.

How do I get the current date in UTC?

Use CAST(GETUTCDATE() AS date) or CAST(SYSUTCDATETIME() AS date) to get the current UTC date without time.

Can I use CURRENT_TIMESTAMP to get the date?

CURRENT_TIMESTAMP returns a datetime value including time. To get only the date, cast it: CAST(CURRENT_TIMESTAMP AS date).

Why does my query return no results when I filter by today's date?

If the column you are filtering includes a time component, an exact match with a date-only value will fail. Use a range: WHERE column >= CAST(GETDATE() AS date) AND column < DATEADD(day, 1, CAST(GETDATE() AS date)).

How do I format the current date as a string?

Use CONVERT(varchar(10), GETDATE(), 23) for ISO format (yyyy-mm-dd), or FORMAT(GETDATE(), 'yyyy-MM-dd') for a customizable string (SQL Server 2012+).