By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,266 Members | 1,312 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,266 IT Pros & Developers. It's quick & easy.

Get all records from Last Two Days?

P: n/a
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
Apr 18 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Try:

WHERE hit_date >= GETDATE() - 2
--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
..
"John" <no******@nope.com> wrote in message
news:7J********************@adelphia.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
Apr 18 '06 #2

P: n/a

"Tom Moreau" <to*@dont.spam.me.cips.ca> wrote in message
news:4S*******************@news20.bellglobal.com.. .
Try:

WHERE hit_date >= GETDATE() - 2
--
Tom
Yes. That's it.

Thanks!

JB



----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
.
"John" <no******@nope.com> wrote in message
news:7J********************@adelphia.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

Apr 18 '06 #3

P: n/a
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
---

Apr 18 '06 #4

P: n/a
John (no******@nope.com) writes:
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, es****@sommarskog.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
Apr 19 '06 #5

P: n/a
Do you mean yesterday and today's data only?

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

Madhivanan

Apr 21 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.