How to extract data between yesterday and today?

1.2K    Asked by behail_3566 in SQL Server , Asked on Jul 12, 2021

I have a column called create date of a DateTime datatype. I want to extract data between today and yesterday. select * from table1(no lock) where createdate ='2018-06-01' --not getting any output I want something like this:


select * from table1(no lock) where createdate between '2018-06-01' and '2018-06-02' Rather than hard coding the date, I need between dateadd(d,-1,getdate()) and getdate(), but getdate() gives current timestamp. How can I search for the whole day like 2018-06-02 rather than 2018-06-02 02:12:13.423?



Answered by Cameron Oliver

You can always find the SQL server current date today using SELECT CONVERT(date, GETDATE());. So to find all of the data for yesterday, you say:

  DECLARE @today date = GETDATE(); SELECT ... WHERE createDate >= DATEADD(DAY, -1, @today) AND createDate < @today;

For today, it's a simple change:

WHERE createDate >= @today AND createDate < DATEADD>


Your Answer