In this comprehensive article, I’ll cover everything you need to know about using dates in SQL Server WHERE clauses, from basic syntax to advanced techniques.
SQL Server Date In Where Clause
The first challenge when working with dates in SQL Server is understanding the available data types.
Common Date Data Types in SQL Server
SQL Server offers several date-related data types:
- DATE: Stores date only (no time component)
- DATETIME: Stores both date and time (accuracy to 3.33 milliseconds)
- DATETIME2: Enhanced version with larger date range and better precision
- SMALLDATETIME: Compact storage with minute precision
- DATETIMEOFFSET: Includes time zone awareness
The ISO-8601 Date Format
When working with date literals in SQL Server, using the ISO-8601 format with the following syntax is recommended.
WHERE OrderDate = '2023-05-26' -- YYYY-MM-DD formatLet’s explore different ways to use the WHERE clause to filter by dates in SQL Server.
Case 1: Filtering for a Specific Date
You can use the following queries when filtering for records on a specific date.
The below query will get you the list of records on the sale_date ‘2025-03-23’.
Select * from sales
WHERE sale_Date = '2025-03-23'After executing the above query, I got the expected output as shown in the screenshot below.

The second approach catches all records from the given date, regardless of the time component.
Case 2: Filtering for a Date Range
For date ranges, use comparison operators:
We can execute the query below to get a specified date range.
Select * from sales
WHERE sale_Date >= '2025-03-23' AND sale_Date < '2025-05-23'I got the expected output after executing the above query as shown in the screenshot below.

Case 3: Filtering for Relative Dates
For dynamic date filtering:
If we wish to get the sales Orders from yesterday, we can execute the following query.
Select * from sales
WHERE sale_Date >= DATEADD(day, -1, CAST(GETDATE() AS date))
AND sale_Date < CAST(GETDATE() AS date)After executing the above query, I got the expected output as shown in the screenshot below.

Similarly, the following query can be used to retrieve the lists of Sales Orders from the last 30 days.
Select * from sales
WHERE sale_Date >= DATEADD(day, -30, GETDATE())After executing the above query, I got the expected output in the screenshot below.

Advanced Date Filtering Techniques
Working with DATETIME Columns When You Only Need the Date
When you have a DATETIME column (suppose the sale_Date column in my case), but only need to filter by the date component:
Select * from sales
WHERE CONVERT(date, sale_Date) = '2025-05-23'After executing the above query, I got the expected output shown in the screenshot below.

Optimizing Date Range Queries
SQL Server can optimize date range queries when appropriately written:
For efficient index seeks, we can execute the following query.
Select * from sales
WHERE sale_Date BETWEEN '2025-05-23' AND '2025-05-23 23:59:59.997'After executing the above query, I got the expected output as shown in the screenshot below.

But for a more transparent and less prone-to-error approach, you can use the query below.
Select * from sales
WHERE sale_Date >= '2025-05-01' AND sale_Date < '2025-06-23'After executing the above query, I got the expected output as shown in the screenshot below.

Working with Specific Time Components
Sometimes, you need to filter by specific times. For example, here, you wish to get the lists of sales orders between 9 and 16. You can use the query below for that purpose.
Select * from sales
WHERE DATEPART(HOUR, cast(cast(sale_Date as date) as datetime)) BETWEEN 9 AND 16After executing the above query, I got the output as expected, as per the screenshot below.

DateTime Functions That Work Well in WHERE Clauses
While applying functions to columns should generally be avoided, there are cases where SQL Server’s built-in functions can be helpful to:
- DATEADD(): Add time intervals to dates
- DATEDIFF(): Calculate differences between dates
- EOMONTH(): Get the last day of the month
Conclusion
As explained above, following these guidelines and the information will help you write more reliable, performant SQL queries for date filtering with the WHERE clause. Below are the lists of the overall summary.
- Use ISO-8601 format (
YYYY-MM-DD) for dates - Avoid functions on columns in WHERE clauses
- Use half-open intervals (
>= start AND < end) for date ranges - Apply operations to parameters, not columns
- Create appropriate indexes on frequently filtered date columns
- Be careful with BETWEEN for datetime values due to precision issues
- Use parameterized queries in applications
- Consider partitioning for very large tables
You may also like the following articles
- SQL Server Date Between
- The datepart hour is not supported by date function dateadd for data type date.
- SQL Server Extract Year From Date
- SQL Server Insert Date
- SQL Server Get Month From Date
After working for more than 15 years in the Software field, especially in Microsoft technologies, I have decided to share my expert knowledge of SQL Server. Check out all the SQL Server and related database tutorials I have shared here. Most of the readers are from countries like the United States of America, the United Kingdom, New Zealand, Australia, Canada, etc. I am also a Microsoft MVP. Check out more here.