Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 94
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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