150

I am looking for a good SQL Statement to select all rows from the previous day from one table. The table holds one datetime column. I am using SQL Server 2005.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rudimenter
  • 3,242
  • 4
  • 33
  • 46

13 Answers13

260

get today no time:

SELECT dateadd(day,datediff(day,0,GETDATE()),0)

get yesterday no time:

SELECT dateadd(day,datediff(day,1,GETDATE()),0)

query for all of rows from only yesterday:

select 
    * 
    from yourTable
    WHERE YourDate >= dateadd(day,datediff(day,1,GETDATE()),0)
        AND YourDate < dateadd(day,datediff(day,0,GETDATE()),0)
Harry Moreno
  • 10,231
  • 7
  • 64
  • 116
KM.
  • 101,727
  • 34
  • 178
  • 212
  • @ashuthinks, your comment makes no sense to me. The original question asks how to get rows from the previous day. To do that you need to be able to get today's date only (no time) and yesterday's date only (no time). You use these (timeless) dates in the` WHERE` clause. However, the `SELECT *` will return any dates with their original times. – KM. Dec 30 '13 at 14:44
  • datediff gives "Incorrect Parameter Count error". and http://stackoverflow.com/a/18926156/3007408 says datediff can use only 2 parameters. any solution?? – Sp0T May 01 '15 at 07:20
  • @Sp0T, this question is tagged **SQL Server**, which has a DATEDIFF() function that accepts three parameters (https://msdn.microsoft.com/en-us/library/ms189794.aspx) the question you link to is for MySql, which I guess works differently as you have found. You will find that SQL is not completely interchangeable, there are many differences like this between different vendors, especially regarding date handling. – KM. May 01 '15 at 13:45
  • Ohh, thanks. Didn't knew that. Btw I solved the issue by using "between curdate() -1 day and curdate()". May be it can be used in this case also. – Sp0T May 01 '15 at 14:25
  • Will this work every 1st of the month to find 'yesterday'? so 2016-02-01 becomes 2016-01-31? – Bidstrup Feb 01 '16 at 12:17
  • 2
    @RasmusBidstrup, yes. when I run `SELECT getdate(),dateadd(day,datediff(day,1,GETDATE()),0)` I get: `2016-02-01 10:27:54.733 2016-01-31 00:00:00.000` – KM. Feb 01 '16 at 15:28
  • WHy use datediff? Just use DATEADD(day, -1, convert(date, GETDATE())) – AquaAlex May 18 '21 at 07:10
59

To get the "today" value in SQL:

convert(date, GETDATE())

To get "yesterday":

DATEADD(day, -1, convert(date, GETDATE()))

To get "today minus X days": change the -1 into -X.

So for all yesterday's rows, you get:

select * from tablename
   where date >= DATEADD(day, -1, convert(date, GETDATE()))
   and date < convert(date, GETDATE())
Konamiman
  • 49,681
  • 17
  • 108
  • 138
  • "date" Datatype doesnt exist in SQL Server 2005. When i use datetime instead the time value stays and the calculation happens not from 0am to 12pm but from the the time you run the Query – rudimenter Oct 01 '09 at 11:35
  • 1
    My mistake. I didn't see you were dealing with SQL Server 2005. Indeed, my code works for SQL Server 2008 only. – Konamiman Oct 01 '09 at 11:50
  • I always thought that DATEADD(day,....) was a waste, just add or subtract the number of days against the date: _SELECT GETDATE()-1_ – KM. Oct 01 '09 at 11:58
20

It's seems the obvious answer was missing. To get all data from a table (Ttable) where the column (DatetimeColumn) is a datetime with a timestamp the following query can be used:

SELECT * FROM Ttable
WHERE DATEDIFF(day,Ttable.DatetimeColumn ,GETDATE()) = 1 -- yesterday

This can easily be changed to today, last month, last year, etc.

call me Steve
  • 1,709
  • 3
  • 18
  • 31
  • 7
    This one works pretty well, but is much more expensive compared to `DatetimeColumn BETWEEN DATEADD(day, DATEDIFF(day, 1, GETDATE()) ,0) AND DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)` as it has to evaluate the DATEDIFF() on each row – Václav Holuša Oct 21 '18 at 20:29
13
SELECT * from table_name where date_field = DATE_SUB(CURRENT_DATE(),INTERVAL 1 DAY);
Romancha KC
  • 1,507
  • 1
  • 13
  • 12
5

Its a really old thread, but here is my take on it. Rather than 2 different clauses, one greater than and less than. I use this below syntax for selecting records from A date. If you want a date range then previous answers are the way to go.

SELECT * FROM TABLE_NAME WHERE 
DATEDIFF(DAY, DATEADD(DAY, X , CURRENT_TIMESTAMP), <column_name>) = 0

In the above case X will be -1 for yesterday's records

Rahul
  • 1,007
  • 8
  • 5
4

This should do it:

WHERE `date` = CURDATE() - INTERVAL 1 DAY
candlejack
  • 1,189
  • 2
  • 22
  • 51
3

Can't test it right now, but:

select * from tablename where date >= dateadd(day, datediff(day, 1, getdate()), 0) and date < dateadd(day, datediff(day, 0, getdate()), 0)
Mark Bell
  • 28,985
  • 26
  • 118
  • 145
2

In SQL Server do like this:

where cast(columnName as date) = cast(getdate() -1 as date)

You should cast both sides of the expression to date to avoid issues with time formatting.

If you need to control interval in more detail, then you should try something like:

declare @start datetime = cast(getdate() - 1 as date)
declare @end datetime = cast(getdate() - 1 as date)
set @end = dateadd(second, 86399, @end)
Mário Meyrelles
  • 1,594
  • 21
  • 26
0

Another way to tell it "Yesterday"...

Select * from TABLE
where Day(DateField) = (Day(GetDate())-1)
and Month(DateField) = (Month(GetDate()))
and Year(DateField) = (Year(getdate()))

This conceivably won't work well on January 1, as well as the first day of every month. But on the fly it's effective.

0

Well, its easier to cast the datetime column to date and than compare.

SELECT * FROM TABLE_NAME WHERE cast(COLUMN_NAME as date) = 
   dateadd(day,0, convert(date, getdate(), 105)) 
Amey P Naik
  • 710
  • 1
  • 8
  • 18
0

A simple alternative

Select GETDATE() - 1

Change 1 to go back that many number of days

PS : This gives you timestamp accuracy.

viru
  • 2,081
  • 19
  • 23
0

This worked a charm:

SELECT * FROM mytable WHERE date(mydate) = DATE_SUB(CURDATE(), INTERVAL 1 DAY);
eva
  • 59
  • 3
-1

subdate(now(),1) will return yesterdays timestamp The below code will select all rows with yesterday's timestamp

Select * FROM `login` WHERE `dattime` <= subdate(now(),1) AND `dattime` > subdate(now(),2)
Dismi Paul
  • 187
  • 1
  • 5