[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 91
  • Last Modified:

SQL Query show items in the last 2 days

How would i do a where clause to show items where the data field is the last 2 days?
0
vbnetcoder
Asked:
vbnetcoder
1 Solution
 
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
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
 
Deepak ChauhanSQL Server DBACommented:
select * from table1
where CONVERT(varchar(10), dt,101) >= CONVERT(varchar(10), (GETDATE()-2),101)
0
 
vbnetcoderAuthor Commented:
ty
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now