[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 88
  • 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 BurtonCommented:
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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