Home > Database > Mysql Tutorial > How to Select Rows from the Previous Day in SQL Server?

How to Select Rows from the Previous Day in SQL Server?

Susan Sarandon
Release: 2025-01-08 17:41:44
Original
650 people have browsed it

How to Select Rows from the Previous Day in SQL Server?

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>
Copy after login

To retrieve yesterday's date (also without the time), use this:

<code class="language-sql">SELECT DATEADD(day, DATEDIFF(day, 1, GETDATE()), 0)</code>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template