• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 434
  • Last Modified:

running a SQL query to retrieve current day -2

Guru's,
I have a sql statement list below where I am selecting from a table converting the date to truncate the time. and retrive the data for today,  Now I need to be able to run this select for the current day and also the the prvious 2 days. I would expect that the getdate funtion -2 would be the answer but i am getting spurattic results. Please advice

SELECT *
FROM DataTable
WHERE JobReleasedDt >= Convert(datetime, Convert(varchar, Getdate(), 101))
0
69stang
Asked:
69stang
  • 2
1 Solution
 
bamboo7431Commented:
SELECT *
FROM DataTable
WHERE JobReleasedDt >= Convert(datetime, Convert(varchar, dateadd(day,-2,Getdate()), 101))
0
 
hongjunCommented:
try this

SELECT *
FROM DataTable
WHERE JobReleasedDt >= getdate()-2
0
 
hongjunCommented:
Or if you want to be from time 00:00, then use below

SELECT *
FROM DataTable
WHERE JobReleasedDt >=cast(convert(varchar(11), getdate(), 101) as datetime)-2
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

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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