Solved

SQL Query show items in the last 2 days

Posted on 2015-01-14
3
70 Views
Last Modified: 2015-01-17
How would i do a where clause to show items where the data field is the last 2 days?
0
Comment
Question by:vbnetcoder
3 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40549268
myDateField>=dateadd(day,-2,GETDATE())

will look for any dates which are up to two days ago. So today is Wednesday 5 p.m., so it will look from Monday 5 p.m.

If you wanted to look from Tuesday (so the two days are Tuesday and Wednesday) from midnight, then

myDateField>=dateadd(day,0,datediff(day,0,dateadd(day,-1,GETDATE())))

will do that.

There are shortcuts in SQL Server 2012 and 2014, but the above should work in 2008.
0
 
LVL 15

Expert Comment

by:deepakChauhan
ID: 40549337
select * from table1
where CONVERT(varchar(10), dt,101) >= CONVERT(varchar(10), (GETDATE()-2),101)
0
 

Author Closing Comment

by:vbnetcoder
ID: 40555400
ty
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now