Efficiently Querying Yesterday's Data with SQL Server
Data extraction for specific timeframes is a common task in database management. This guide shows how to retrieve records from the previous day using SQL Server, providing a robust solution for accurate data retrieval.
To get today's date (without the time component), use this SQL query:
<code class="language-sql">SELECT DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)</code>
To retrieve yesterday's date (also without the time), use this:
<code class="language-sql">SELECT DATEADD(day, DATEDIFF(day, 1, GETDATE()), 0)</code>
Finally, to select all rows from a table named "YourTable" with a "YourDate" datetime column that fall within yesterday's date range, use the following query:
<code class="language-sql">SELECT * FROM YourTable WHERE YourDate >= DATEADD(day, DATEDIFF(day, 1, GETDATE()), 0) AND YourDate < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)</code>
This query effectively filters data based on the date, ensuring only yesterday's entries are returned.
The above is the detailed content of How to Select Rows from the Previous Day in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!