dbTalk Databases Forums  

Get all records from Last Two Days?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Get all records from Last Two Days? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
John
 
Posts: n/a

Default Get all records from Last Two Days? - 04-18-2006 , 05:27 PM






Hi Everyone,

I use the following to get records from the last two days in MySql:
where date_entered <= curdate() and date_entered >=
DATE_SUB(curdate(),INTERVAL 2 day)

I'm looking to do the same in MS-Sql server but I'm just not getting it.
I've got this so far which does not work:
where hit_date <= GETDATE() and hit_date >= DATE_SUB(GETDATE(),INTERVAL 2
day)

then I tried this:
WHERE hit_date >= DATEDIFF(GETDATE(), (GETDATE()-2)>

Essentially, I need all records from the last two days.

Any help or guidance in this matter would be greatly appreciated.

-JohnyB



Reply With Quote
  #2  
Old   
Tom Moreau
 
Posts: n/a

Default Re: Get all records from Last Two Days? - 04-18-2006 , 05:36 PM






Try:

WHERE hit_date >= GETDATE() - 2


--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
..
"John" <nothanks (AT) nope (DOT) com> wrote

Hi Everyone,

I use the following to get records from the last two days in MySql:
where date_entered <= curdate() and date_entered >=
DATE_SUB(curdate(),INTERVAL 2 day)

I'm looking to do the same in MS-Sql server but I'm just not getting it.
I've got this so far which does not work:
where hit_date <= GETDATE() and hit_date >= DATE_SUB(GETDATE(),INTERVAL 2
day)

then I tried this:
WHERE hit_date >= DATEDIFF(GETDATE(), (GETDATE()-2)>

Essentially, I need all records from the last two days.

Any help or guidance in this matter would be greatly appreciated.

-JohnyB



Reply With Quote
  #3  
Old   
John
 
Posts: n/a

Default Re: Get all records from Last Two Days? - 04-18-2006 , 05:56 PM




"Tom Moreau" <tom (AT) dont (DOT) spam.me.cips.ca> wrote

Quote:
Try:

WHERE hit_date >= GETDATE() - 2


--
Tom
Yes. That's it.

Thanks!

JB





Quote:
----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
.
"John" <nothanks (AT) nope (DOT) com> wrote in message
news:7JKdnbE0F6Nc-tjZRVn-uQ (AT) adelphia (DOT) com...
Hi Everyone,

I use the following to get records from the last two days in MySql:
where date_entered <= curdate() and date_entered >=
DATE_SUB(curdate(),INTERVAL 2 day)

I'm looking to do the same in MS-Sql server but I'm just not getting it.
I've got this so far which does not work:
where hit_date <= GETDATE() and hit_date >= DATE_SUB(GETDATE(),INTERVAL
2
day)

then I tried this:
WHERE hit_date >= DATEDIFF(GETDATE(), (GETDATE()-2)

Essentially, I need all records from the last two days.

Any help or guidance in this matter would be greatly appreciated.

-JohnyB





Reply With Quote
  #4  
Old   
Jens
 
Posts: n/a

Default Re: Get all records from Last Two Days? - 04-18-2006 , 06:02 PM



Use the dateadd (Or Datediff) for that:

hit_date >= DATEADD(d,-2,GETDATE()), keep in mind that this will
substract the days including the current time, so a 04/19/2006 1:09
will result in 04/17/2006 1:09.

HTH, Jens Suessmeyer.

---
http://www.sqlserver2005.de
---


Reply With Quote
  #5  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Get all records from Last Two Days? - 04-19-2006 , 04:50 PM



John (nothanks (AT) nope (DOT) com) writes:
Quote:
I use the following to get records from the last two days in MySql:
where date_entered <= curdate() and date_entered >=
DATE_SUB(curdate(),INTERVAL 2 day)

I'm looking to do the same in MS-Sql server but I'm just not getting it.
I've got this so far which does not work:
where hit_date <= GETDATE() and hit_date >= DATE_SUB(GETDATE(),INTERVAL
2
day)

then I tried this:
WHERE hit_date >= DATEDIFF(GETDATE(), (GETDATE()-2)

Essentially, I need all records from the last two days.
Since you appear to be trying out syntax at random, I must ask: did
you ever consider to consult Books Online?



--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #6  
Old   
Madhivanan
 
Posts: n/a

Default Re: Get all records from Last Two Days? - 04-21-2006 , 05:55 AM



Do you mean yesterday and today's data only?

Select * from yourtable
where datecol>=Dateadd(day,datediff(day,o,getdate()),-1)

Madhivanan


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.