Solved

Get rows from today and yesterday

Posted on 2011-03-23
5
707 Views
Last Modified: 2012-05-11
This is SQL 2000

I have a table with rows dated today and prior to today. I want to get the rows dated today and yesterday. I tried the 2 below sqls and I dont get any rows back:

select  * from table where  convert(varchar(10),updatedate, 101) >= convert(varchar(10),getdate(), 101)
 and convert(varchar(10),updatedate, 101) <= convert(varchar(10),getdate() -1 , 101)
/*
 where   DATEADD(dd, 0, DATEDIFF(dd, 0, updatedate)) >= DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
 and DATEADD(dd, 0, DATEDIFF(dd, 0, updatedate)) <= DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()- 1))
*/
 order by updatedate desc

Open in new window

0
Comment
Question by:Camillia
  • 2
  • 2
5 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 35200106
try
where updatedate >= DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
or updatedate > DATEADD(dd, 0, DATEDIFF(dd, 0, DATEADD(DD, -1, GETDATE())))

Open in new window

0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35200110
don't convert anything.

select * from mytable where updatedate > getdate() - 2 order by updatedate desc

That will give you everything updated in the last 2 days but include the time in that. If you don't want the time element included:

select * from mytable where cast(convert(nvarchar(10), updatedate, 101) as datetime) > getdate() - 2order by updatedate desc
0
 
LVL 13

Expert Comment

by:Rick
ID: 35200151
-- Today's:
select * from table where convert(varchar(20), [updatedate], 101) = convert(varchar(20), getdate(), 101)

-- Yesterday's:
select * from table where convert(varchar(20), [updatedate], 101) = convert(varchar(20), dateadd(day, -1, getdate()), 101)
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 400 total points
ID: 35200180

You just need

where updatetdate >= DATEADD(dd, 0, DATEDIFF(dd, 0, DATEADD(DD, -1, GETDATE())))
0
 
LVL 13

Assisted Solution

by:Rick
Rick earned 100 total points
ID: 35200232
Or if you want to show yesterdays and todays together:

select * from table where [updatedate] between convert(varchar(20), dateadd(day, -1, getdate()), 101) and convert(varchar(20), getdate(), 101)


But ewangoya's solution is better (short and effective).
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question