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

How to Select All Rows from the Previous Day Using SQL Server?

Linda Hamilton
Release: 2025-01-08 17:46:40
Original
731 people have browsed it

How to Select All Rows from the Previous Day Using SQL Server?

SQL Server gets all the records of the previous day

This article describes how to query all records of the previous day in a table containing a datetime column in SQL Server. The following SQL statements apply to SQL Server 2005 and later:

<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 statement selects records from the YourTable table where the YourDate column is greater than or equal to midnight of the previous day and less than midnight of the current day.

Explanation of statement:

  • DATEADD(day, DATEDIFF(day, 1, GETDATE()), 0): This expression returns the start time of the previous day (midnight).
  • DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0): This expression returns the start time of the day (midnight).

Filter by this condition to ensure that only records from the previous day are obtained, excluding records from the current day and earlier dates.

The above is the detailed content of How to Select All Rows from the Previous Day Using 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